Set environment variable with space in Linux

LinuxEnvironment Variables

Linux Problem Overview


I want to set an environment variable that has space in it. it is a path to a folder and the folder name is: /home/mehrabib/my video

I edit .bashrc and add the following line to it:

export $VIDEO=/home/mehrabib/my\ video

and run these commands:

echo $VIDEO
cd $VIDEO

the result is:

/home/mehrabib/my video
/home/mehrabib/my :no such file or directory

I change it to

export $VIDEO=/home/mehrabib/my\\\ video

and run these commands:

echo $VIDEO
cd $VIDEO

the result is:

/home/mehrabib/my\ video
/home/mehrabib/my\ :no such file or directory

what should i do?

Linux Solutions


Solution 1 - Linux

You should do

export VIDEO="/home/mehrabib/my video"

and to sum Dan's comments up also do

cd "$VIDEO"

which will expand to

cd "/home/mehrabib/my video"

again.

Personally, I've come to prefer the ${VIDEO} syntax.

Solution 2 - Linux

You can also substitute special characters - use * as a wildcard to substitute for the space.

VIDEO="/home/mehrabib/m*o"

Solution 3 - Linux

Try to quote VIDEO: cd "$VIDEO".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBabak MehrabiView Question on Stackoverflow
Solution 1 - LinuxNodebodyView Answer on Stackoverflow
Solution 2 - LinuxagencyView Answer on Stackoverflow
Solution 3 - LinuxcnicutarView Answer on Stackoverflow