What is the difference between <EOF and <'EOF' in heredocs shell?

I am writing a .spec file for a module for a linux build system and have encountered a small problem and wanted to share it.

To write a script file:

cat <<EOF > /path/to/somewhere/script #blah blah EOF chmod +x script 

When launching the script on the target side, errors occurred indicating the location of the script, as it was on the host system. The base size of $ 0 was incorrect.

Fixed by changing the first line as follows after viewing the sample code on the Internet:

 cat <<'EOF' > /path/to/somewhere/script #blah blah EOF chmod +x script 

I wonder what the difference is and what made him work a second time.

+6
source share
2 answers

The difference is that in this version:

 <<EOF ... EOF 

... functions roughly like a double-quoted string, expanding parameters and substituting commands, etc. (in particular, in your case, replacing $0 with the value $0 ), whereas in this version:

 <<'EOF' ... EOF 

... functions roughly like a single-quoted string, and such decompositions are not performed.

(See & sect; 3.6.6 โ€œDocuments Hereโ€ in the Bash reference manual .)

+14
source

The difference is whether dollars and back quotes are interpreted in the text of the document here, since the document is expanded here.

 $ XYZ=pqr $ cat <<EOF > echo $XYZ > EOF echo pqr $ cat <<'EOF' > echo $XYZ > EOF echo $XYZ $ 

You can try similar tricks with $(...) .

+3
source

Source: https://habr.com/ru/post/910763/


All Articles