Escaping Variable in Cat

5

1

I'm trying to write a shell script over ssh via a bash prompt. The shell, however, insists on interpreting any variable I want to write instead of writing it directly to file. For example, cat <<EOF >checkup.sh\n'$command'EOF is simply written as '' to file. How do I get $command written instead? I've tried every practical method of escaping I can think of.

If it changes anything, I'm doing it over PHP using phpseclib.

Peter Kazazes

Posted 2012-11-02T04:58:11.327

Reputation: 205

Answers

7

Try quoting the first EOF, e.g.,

cat <<'EOF'
>checkup.sh\n'$command'
EOF

This is explained in the bash(1) man page, in the section, Here Documents.

garyjohn

Posted 2012-11-02T04:58:11.327

Reputation: 29 085

I actually needed the INVERSE of this (had a script with "EOF", needed to handle variables) and this pointed me at it.

Thanks! – Samurai Ken – 2012-12-04T07:06:23.783