Admittedly, I’m a bash neophyte. I always want to reach for Python for my shell scripting purposes. However, I’m trying to push myself to learn some bash. I’m curious why the following code doesn’t work.
sh -c "F=\"123\"; echo $F"
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It doesn’t work because variable expansion in the double-quoted string happens before the command is called. That is, if I type:
The shell transforms this into:
Before actually calling the echo command. Similarly, if you type:
This gets transformed into:
Before calling a the
shcommand. You can use single quotes to inhibit variable expansion, for example:You can also escape the
$with a backslash: