bash Shell Scripting:使用变量定义路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8950695/
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-09-09 21:32:13  来源:igfitidea点击:

Shell Scripting: Using a variable to define a path

bashshellvariablespath

提问by Nonameghost

My problem lies with my confusion with shell variables.

我的问题在于我对shell variables 的困惑。

To my understanding, variables allow me to store a value (String in this case) and to call it later in my code. So if I wanted to have a variable that holds the pathto some set of scripts, I could ideally just store it like this:

据我了解,变量允许我存储一个值(在本例中为 String)并稍后在我的代码中调用它。因此,如果我想要一个变量来保存某些脚本集的路径,那么理想情况下我可以像这样存储它:

SPTH = '/home/Foo/Documents/Programs/ShellScripts/Butler'

//Later on in the script//
cd $SPTH
./script1

What I'm trying to do, with probably the wrong syntax, is to set the path to variable SPTH.

我试图做的,可能是错误的语法,是将路径设置为variableSPTH

Then I use cdwith argument $SPTH.

然后我使用cdwith argument $SPTH

Ideally this would allow me to run the file there without typing in the path. However it doesn't work. The $SPTHis ignored and the result is as if cdwas used alone.

理想情况下,这将允许我在不输入路径的情况下在那里运行文件。但是它不起作用。该$SPTH被忽略,结果是,如果cd是单独使用。

So what am I doing wrong? And what would be a way to do this?

那么我做错了什么?有什么办法可以做到这一点?

回答by Web User

Don't use spaces...

不要使用空格...

(Incorrect)

(不正确)

SPTH = '/home/Foo/Documents/Programs/ShellScripts/Butler'

(Correct)

(正确的)

SPTH='/home/Foo/Documents/Programs/ShellScripts/Butler'