LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Export path with spaces - escape characters and quotes not working - Ubuntu 10.04 (https://www.linuxquestions.org/questions/linux-newbie-8/export-path-with-spaces-escape-characters-and-quotes-not-working-ubuntu-10-04-a-927468/)

clustro 02-03-2012 05:47 PM

Export path with spaces - escape characters and quotes not working - Ubuntu 10.04
 
Hi Linux Questions,

For some reason, escape characters and quotes don't seem to work anymore when trying to export a new path variable. Observe a few lines from my .bash_profile file.

export DESKTOP="/home/abhay/Desktop"
export RCGSHARE="/home/abhay/Desktop/rcgshare"
export MCSA="$DESKTOP/GA\ and\ MCSA\ Projects/mcsa\ project"
export WORKSPACE="$DESKTOP/workspace"

$DESKTOP, $RCGSHARE, and $WORKSPACE all work correctly. They have no spaces

$MCSA gives:
abhay@jcmaxwell:~/Desktop/workspace$ $MCSA
bash: /home/abhay/Desktop/GA\: No such file or directory

Now we'll try

export MCSA="$DESKTOP/'GA and MCSA Projects'/'mcsa project'"

abhay@jcmaxwell:~$ source .bash_profile
abhay@jcmaxwell:~$ $MCSA
bash: /home/abhay/Desktop/'GA: No such file or directory

Again, it doesn't work.

But the quotes and escape characters work when trying to access a folder directly! Look! :(

abhay@jcmaxwell:~/Desktop$ cd 'Dec 2011 Tasks'
abhay@jcmaxwell:~/Desktop/Dec 2011 Tasks$
abhay@jcmaxwell:~/Desktop/Dec 2011 Tasks$ cd ..
abhay@jcmaxwell:~/Desktop$ cd Dec\ 2011\ Tasks
abhay@jcmaxwell:~/Desktop/Dec 2011 Tasks$

What is wrong? What am I missing that is causing this to happen?

I am using Ubuntu 10.04.

impert 02-03-2012 06:05 PM

Try using single quotes.
Code:

export MCSA='$DESKTOP/GA and MCSA Projects/mcsa project'
Works for me.

David the H. 02-04-2012 07:29 AM

Use either quote-marks or backslashes, but not both, as they do the same thing.

The purpose of quoting and backslashing is to tell the shell to ignore characters that usually have reserved meanings, and to only parse their literal values.

Note that double-quotes do allow certain expansions to occur, one of which is backslash-escapes. But as the bash man page explains: "The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or <newline>." So a backslash followed by a space character will be treated literally, when enclosed in double-quotes.

Once a string is stored in a variable, all characters in it retain their literal values only, with the main exception being that unquoted variable strings are still subject to word splitting and glob expansion.

See here for more on how the shell parses whitespace and quotes:

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


All times are GMT -5. The time now is 01:42 PM.