Loading...

Inline bash script variables

Answer #1 100 %

It doesn't work because variable expansion in the double-quoted string happens before the command is called. That is, if I type:

echo "$HOME"

The shell transforms this into:

echo "/home/lars"

Before actually calling the echo command. Similarly, if you type:

sh -c "F=\"123\"; echo $F"

This gets transformed into:

sh -c "F=\"123\"; echo"

Before calling a the sh command. You can use single quotes to inhibit variable expansion, for example:

sh -c 'F="123"; echo $F'

You can also escape the $ with a backslash:

sh -c "F=\"123\"; echo \$F"
Tags: bash

You’ll also like:


© 2023 CodeForDev.com -