Problem :
I wanted to do a bash function that given a text variable, it would concatenate it to a file. i.e
function print() {
cat << 'EOF' >> file
This $1 is a variable
EOF
}
But it wouldnt work. It would always output This $1 is a variable
.
I tried ${1}
, {$1}
, '$1'
, "$1"
,and combinations of those, none worked. Is there some sort of special syntax, or should I use another command… what should I do?
Solution :
Unquote EOF:
#!/usr/bin/env bash
function print() {
cat << EOF >> file
This $1 is a variable
EOF
}
print stuff
In man bash
it says under Here Documents
:
If word is unquoted, all lines of the here-document are subjected to
parameter expansion, command substitution, and arithmetic expansion,
the character sequence <newline> is ignored, and must be used to
quote the characters , $, and `.