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.
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 .)