bash 脚本中的 ${0%/*} 是什么意思? [英] 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(即调用脚本的路径)中包含的值string /*
从它的末尾开始.
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}
.正如我所说的 $0
是特殊的,因为它不是 真正的 参数,它总是存在并代表脚本的名称.参数扩展在 {
}
-- 大括号内起作用,而 %
是参数扩展的一种类型.
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 occurrence 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
这篇关于bash 脚本中的 ${0%/*} 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!