I was messing around with a log4j properties file and accidently made a folder with the following text ${foo} however I also have an environment variable named foo that points to a folder so thus if I do rm -rf "${foo}" it removes the folder $foo is pointing to, instead of the folder ${foo}. How can I specify to delete the folder in my current directory using a relative path instead of deleting the folder the environment variable points to?

Here is the layout to better help understand

$foo = /home/user/bar

${foo} = /home/user/${foo}

  • What is content of $foo? – cuonglm Dec 16 '14 at 17:03
  • @cuonglm the content of $foo is a folder called bar so if you entered $foo and did tab complete it would come up as /home/user/bar – jgr208 Dec 16 '14 at 17:04
  • Use single quotes. – muru Dec 16 '14 at 17:16
  • @muru so like this '${foo}'? – jgr208 Dec 16 '14 at 17:17
  • @jgr208 if the folder is named ${foo}, yep. – muru Dec 16 '14 at 17:20

String interpolation causes this. There are a number of ways to selectively prevent this from happening. The bash hackers wiki has some good examples, though the specifics may vary if you're not actually using bash.

In short, you can prevent interpolation with single quotes, or you can escape the characters.

[me:~/work]$ export foo=bar
[me:~/work]$ echo $foo
[me:~/work]$ echo "\${foo} is set to ${foo}"
${foo} is set to bar
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.