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
Shell Scripting: Using a variable to define a path
提问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 cd
with argument $SPTH
.
然后我使用cd
with argument $SPTH
。
Ideally this would allow me to run the file there without typing in the path. However it doesn't work. The $SPTH
is ignored and the result is as if cd
was 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'