Reputation: 159
I'm writing a .spec file for a module for linux build system and came across a small issue and wanted to share it.
For writing a script file :
cat <<EOF > /path/to/somewhere/script
#blah blah
EOF
chmod +x script
When the script ran on the target there were errors pointing to the location of the script as it were in the host system.Basically $0 was wrong.
Fixed it by changing the first line like this after seeing some sample code online:
cat <<'EOF' > /path/to/somewhere/script
#blah blah
EOF
chmod +x script
Wondering what's the difference and what made it work the second time.
Upvotes: 8
Views: 19766
Reputation: 117
EOF
will expand $
$()
'EOF'
or "EOF"
won't expand anything
Most careful thing is, all of EOF
'EOF'
"EOF"
don't expand *
( Unless there is *
in the result and you unfortunately echo
it )
Upvotes: 0
Reputation: 754030
The difference is whether the dollars and back quotes in the body of the here document are interpreted as the here document is expanded.
$ XYZ=pqr
$ cat <<EOF
> echo $XYZ
> EOF
echo pqr
$ cat <<'EOF'
> echo $XYZ
> EOF
echo $XYZ
$
You can try similar tricks with $(...)
.
Upvotes: 6
Reputation: 183361
The difference is that in this version:
<<EOF
...
EOF
the ...
functions roughly as a double-quoted string, performing parameter-expansions and command-substitutions and so on (specifically, in your case, replacing $0
with the value of $0
), whereas in this version:
<<'EOF'
...
EOF
the ...
functions roughly as a single-quoted string, and no such expansions are performed.
(See §3.6.6 "Here Documents" in the Bash Reference Manual.)
Upvotes: 24