Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set environment variable with space in Linux

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?

like image 667
Babak Mehrabi Avatar asked Oct 12 '11 13:10

Babak Mehrabi


People also ask

How do I set an environment variable with spaces?

The command is set and the parameter is variable=value . As for most commands and applications it is possible and often required to surround a parameter with double quotes if containing 1 or more spaces or any other character from this list: &()[]{}^=;! '+,`~ .

What is Setenv in Linux?

setenv is a built-in function of the C shell (csh). It is used to define the value of environment variables. If setenv is given no arguments, it displays all environment variables and their values. If only VAR is specified, it sets an environment variable of that name to an empty (null) value.


2 Answers

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.

like image 173
Nodebody Avatar answered Sep 20 '22 00:09

Nodebody


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

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

like image 29
agency Avatar answered Sep 22 '22 00:09

agency