什么是$ {0%/ *}在bash脚本的含义是什么? [英] What is the meaning of ${0%/*} in a bash script?
问题描述
我想了解一个测试脚本,其中包括以下环节:
I am trying to understand a test script, which includes the following segment:
SCRIPT_PATH=${0%/*}
if [ "$0" != "$SCRIPT_PATH" ] && [ "$SCRIPT_PATH" != "" ]; then
cd $SCRIPT_PATH
fi
什么是 $ {0%/ *}
代表什么?谢谢
推荐答案
It is called Parameter Expansion
. Take a look at this page and the rest of the site.
什么 $ {0%/ *}
所做的就是,它扩展包含在价值的参数0 的(这是路径称为取出后的字符串脚本) / *
从它的结束后缀。
What ${0%/*}
does is, it expands the value contained within the argument 0 (which is the path that called the script) after removing the string /*
suffix from the end of it.
因此, $ 0
相同 $ {0}
这就像任何其他的说法,例如。 $ 1
您可以为 $写{1}
。正如我所说的 $ 1,0
是特殊的,因为它不是一个的真正的说法,它总是在那里,并重新presents脚本的名称。参数扩建工程中的 {
}
- 花括号和%
是一个类型参数扩展的。
So, $0
is the same as ${0}
which is like any other argument, eg. $1
which you can write as ${1}
. As I said $0
is special, as it's not a real argument, it's always there and represents name of script. Parameter Expansion works within the {
}
-- curly braces, and %
is one type of Parameter Expansion.
%/ *
匹配 /
的最后次数,并删除任何东西( *
表示字符后的任何东西)。看看这个简单的例子:
%/*
matches the last occurance of /
and removes anything (*
means anything) after that character. Take a look at this simple example:
$ var="foo/bar/baz"
$ echo "$var"
foo/bar/baz
$ echo "${var}"
foo/bar/baz
$ echo "${var%/*}"
foo/bar
这篇关于什么是$ {0%/ *}在bash脚本的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!