2

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?

1 Answer 1

3

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .