Set environment variable with space in Linux

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7740683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:38:57  来源:igfitidea点击:

Set environment variable with space in Linux

linuxenvironment-variables

提问by babak6

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 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:

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

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

and run these commands:

and run these commands:

echo $VIDEO
cd $VIDEO

the result is:

the result is:

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

I change it to

I change it to

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

and run these commands:

and run these commands:

echo $VIDEO
cd $VIDEO

the result is:

the result is:

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

what should i do?

what should i do?

采纳答案by Nodebody

You should do

You should do

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

and to sum Dan's comments up also do

and to sum Dan's comments up also do

cd "$VIDEO"

which will expand to

which will expand to

cd "/home/mehrabib/my video"

again.

again.

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

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

回答by cnicutar

Try to quote VIDEO: cd "$VIDEO".

Try to quote VIDEO: cd "$VIDEO".

回答by agency

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

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

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

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