How to print ENV VAR name to file, NOT VALUE from linux cli

139 views Asked by At

I have this:

cat > ~/add_api_txt_hook.sh <<EOF 
#!/bin/bash 
... 
echo $MY_ENV_VAR
EOF

MY_ENV_VAR is not set when this file is made. So it makes it "" in the file.

I want the file contents to be

#!/bin/bash 
... 
echo $MY_ENV_VAR

NOT:

#!/bin/bash 
... 
echo ""

How do I print the actual ENV VAR KEY to the file? I have searched the internet, but it only returns how to print the value to the file.

Tried google and various syntax around the key (), "", {} etc...

1

There are 1 answers

0
Paul Hodges On

There are several ways to accomplish this.
Obviously, as SiKing pointed out, you can just backslash-escape the dollar sign.

You can also quote the EOF itself. While

$: cat >x <<EOF
echo $PATH
EOF

puts the value of $PATH (including being empty, as in your case) in the file,

$: cat >x <<'EOF'
echo $PATH
EOF

does not:

$: cat x
echo $PATH

Likewise, you can use a "here-string":

$: cat <<<'echo $PATH'
echo $PATH

But be aware that where a here-doc behaves much the same whether single- or double-quotes are used -

$: cat <<"EOF"
echo $PATH
EOF
echo $PATH

here-strings differentiate; single-quotes don't expand variables, but doubles do.

$: cat <<<"echo $PATH"
echo /c/Users/....(redacted)