Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to shell scripting and stepped through this quick guide:

which was helpful but left a lot of questions about working with variables unanswered. My current frustration is trying to use a variable that stores a path in commands.

For example, the following script:


iTunesU_Source="~/Music/iTunes/iTunes\ Media/iTunes\ U/"
ls -al $iTunesU_Source

ls -al $iTunesU_Source_symlink

ls -al ~/Music/iTunes/iTunes\ Media/iTunes\ U/

Prints out:

ls: Media/iTunes\: No such file or directory
ls: U/: No such file or directory
ls: ~/Music/iTunes/iTunes\: No such file or directory
ls: ~/Music/iTunes_U/: No such file or directory
total 0
drwxr-xr-x   7 me  staff  238 Mar  8 10:06 .
drwxr-xr-x  11 me  staff  374 Mar  8 11:13 ..
drwxr-xr-x  27 me  staff  918 Mar  8 08:44 Game Theory - Audio
drwxr-xr-x   3 me  staff  102 Mar  8 10:03 Introduction to Algorithms
drwxr-xr-x  23 me  staff  782 Mar  8 12:17 Machine Learning
drwxr-xr-x   3 me  staff  102 Mar  8 10:06 Machine Learning-Stanford

How do I get the command (ls/cd/etc...) to accept the value of the variable like it does for direct input?

share|improve this question
up vote 1 down vote accepted

The simple solution:

iTunesU_Source="~/Music/iTunes/iTunes Media/iTunes U/"

Why is that? In this line:

iTunesU_Source="~/Music/iTunes/iTunes\ Media/iTunes\ U/"

You're setting the value of the variable to a quoted string. Within quotes, whitespace is already preserved. So, the contents of your variable will be, literally:

~/Music/iTunes/iTunes\ Media/iTunes\ U/

The backslashes are only needed to escape whitespace in unquoted strings, so you could have almost written this like:

iTunesU_Source=~/Music/iTunes/iTunes\ Media/iTunes\ U/

The difference is that in the first command, the ~ is not expanded by shell to your home directory, whereas in the second command your variable iTunesU_Source is assigned /Users/username. So in that case, your variable holds:

/Users/username/Music/iTunes/iTunes\ Media/iTunes\ U/

Read more about quoting here:

share|improve this answer
awesome, thanks. For whatever reason, it still doesn't work with the full path, but the symlink i made works now that I removed the quotes. – baiano Mar 8 '13 at 20:57
You're welcome! What "doesn't work" about the full path? Do you get any error message? – slhck Mar 8 '13 at 21:04
must have been a user error. i recreated it to try and get the error but not it is working, of course ;) – baiano Mar 8 '13 at 21:14
Note that this line: ls -al $iTunesU_Source should be ls -al "$iTunesU_Source" as well. – chepner Mar 8 '13 at 21:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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