Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.bashrc does not read environment path correctly with spaces

In my Ubuntu 13 I have edited my .bashrc adding an environment path:

export OGRE_ANDROID_ROOT=/home/piperoman/Librerias/Ogre\ Android\ SDK

If I echo the variable, it works fine, but when I try to use it in a makefile, it does not. I have tested with a cd command and this is the result:

$ echo $OGRE_ANDROID_ROOT 
/home/piperoman/Librerias/Ogre Android SDK
$ cd $OGRE_ANDROID_ROOT
bash: cd: /home/piperoman/Librerias/Ogre: No such file or directory

Why does echo work but I cannot use the variable correctly with commands?

like image 802
vgonisanz Avatar asked Jun 19 '13 08:06

vgonisanz


People also ask

How do I handle a space in a Bash path?

To cd to a directory with spaces in the name, in Bash, you need to add a backslash ( \ ) before the space. In other words, you need to escape the space.

How do you handle a space in a path in Linux?

The solutions are to use quotes or the backslash escape character. The escape character is more convenient for single spaces, and quotes are better when there are multiple spaces in a path. You should not mix escaping and quotes.

How do I set environment variables in Bashrc?

In order to set a permanent environment variable in Bash, you have to use the export command and add it either to your “. bashrc” file (if this variable is only for you) or to the /etc/environment file if you want all users to have this environment variable.


1 Answers

Short-answer: Word-splitting.

When you have this

export OGRE_ANDROID_ROOT=/home/piperoman/Librerias/Ogre\ Android\ SDK

The environmental variable contains "/home/piperoman/Librerias/Ogre Android SDK".

If you use it without enclosing in quotes, Bash will split the string into words based on the IFS environment variable – by default tab, space, and newlines.

So

cd $OGRE_ANDROID_ROOT

is equivalent to

cd "/home/piperoman/Librerias/Ogre" "Android" "SDK"

So you should quote it, i.e. "$OGRE_ANDROID_ROOT"

like image 53
doubleDown Avatar answered Nov 06 '22 09:11

doubleDown