Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-01-2012, 07:51 PM   #1
LQ Newbie
Registered: Jan 2012
Posts: 4

Rep: Reputation: Disabled
Shell script variable question

I couldn't think of a better title, sorry.

I am trying to figure out a way to go about this, but every time I try, I can't seem to figure it out. Basically, I need to be able to use a shell variable in a command, and the variable is a path to a file.

Let's say the path to the directory is "~/foobar/", so foo="~/foobar/"
This works well enough, it is fine to say "cd $foo" and it will work properly. The problem lies when the path is "~/foo bar/". Since this script will be used in the install on an Android phone, and anyone installing my ROM can install it from any location, I cannot be positive that they will not place it in a directory that has a space in the name. If foo="~/foo bar/" then running the command cd "$foo" will not work. So I have tried a couple ways to make it work, such as:

foo="~/foo bar/"
foo=$(echo $foo | sed 's| |\\ |g')
echo $foo
~/foo\ bar/
foo="~/foo bar/"
echo $foo
'~/foo bar/'
Neither of those attempts work, and I think it is because, for the first one, it tries to cd to a directory named exactly "~/foo\ bar/", instead of using the "\" to make the space part of the name of the directory, "~/foo bar/"

And the same reason applies to the second one, as it tries to cd to a directory that is named exactly "'~/foo bar/'" instead of, again, using the "'" to ake the space part of the name of the directory, "~/foo bar/".

At this point I am basically out of ideas, any help would be awesome.
Old 06-01-2012, 08:51 PM   #2
LQ Newbie
Registered: Jun 2012
Posts: 1

Rep: Reputation: Disabled
This is on my android device:

sh-4.1# foo="/mnt/sdcard/foo bar"
foo="/mnt/sdcard/foo bar"
sh-4.1# mkdir "$foo"
mkdir "$foo"
sh-4.1# pwd
sh-4.1# cd "$foo"
cd "$foo"
sh-4.1# pwd
/mnt/sdcard/foo bar
cd $foo should be change to cd "$foo"
Old 06-01-2012, 10:41 PM   #3
Nominal Animal
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947
Like builderbob said, your quoting is off is all. I very warmly recommend you read the Quoting chapter in the Bash Reference Manual -- POSIX shells and Bourne shells use pretty much the same rules, so those are a very good starting point.

Trust me: learning the proper quoting rules, and applying them liberally, will save you from a lot of typical scripting errors. It is worth every second you need to spend.

To show you why it is important:
somepath=~/"foo bar"/"other"
cd "$somepath"
You do not need to quote the complete path, you are allowed to quote just the parts that need quoting. In the above case, ~ is special to the shell, so you don't want to quote it.

Other ways you can specify the same path:
somepath=~/foo\ bar/other
somepath=~/foo" "bar/other
somepath=~/foo' 'bar/other
somepath=~"/foo bar/other"
somepath=~'/foo bar/other'
somepath=~'/foo bar/'"other"
There are a lot of variations. Just remember that ~ is special to the shell; it does not work if it is part of a string. (If specified on a command line, remember that the shell interprets the command line first too, before running the actual command. All ~ will be transformed to actual paths in the strings the command gets.)

It is a bit more portable to use the HOME environment variable instead of relying on shell ~ support -- i.e. somepath="$HOME/foo bar/other" . However, shells expand ~user to the home directory for user user, which is pretty cumbersome in the general case: $(getent passwd user | cut -d : -f 6) .

Lets show some of the benefits of knowing the proper quoting rules.

Let's assume you have a directory named foo bar in your home directory, and you want to loop over all its immediate subdirectories using the shell. In general, you do this via e.g.
for oneitem in ~/"foo bar"/* ; do
    [ -d "$oneitem" ] || continue
    printf 'oneitem is \047%s\047.\n' "$oneitem"
where the test skips those oneitems that are not directories. Note that if the directory only has files or subdirectories starting with a ., then the glob pattern does not match anything. Shells typically then return the pattern itself. (Bash has a nullglob shell option, which makes it return nothing when the pattern does not match. It is very useful, if you use Bash.)

If you use Bash and create a very funnily named file,
touch ~/foo\ bar/more$'\nthan\tone 'line
and run the above loop, you'll see how the shell manages even very weird file names correctly, if you just apply the quoting rules (and filename expansion rules too in this case).

To remove the funny-named file, run rm -f ~/$'foo bar/more\nthan\tone line' in Bash. (In Bash, $'...' evaluates to ... as if it was single-quoted, but with C-type character escapes converted to their respective characters.)
Old 06-01-2012, 11:56 PM   #4
LQ Newbie
Registered: Jan 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Ahh, I see my problem. I did use double quotes, I was simply testing it in linux and not the phone itself, so I was including the "~/" and that was messing me up. Thanks for the help.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
conky question: passing conky-variable to shell-script zlin50 Linux - Software 2 12-29-2012 06:47 PM
quick variable question in a shell script yargk Linux - Newbie 4 02-23-2010 02:09 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
shell script question $variable in loop icecubeflower Linux - Newbie 2 03-31-2009 09:09 AM
Shell-script question about getting a number as a variable stormrider_may Programming 9 03-14-2006 08:18 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:50 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration