Bash:用字符串操作(百分号)

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

Bash: manipulating with strings (percent sign)

stringbashparameters

提问by musthero

What does a percent sign mean in bash when manipulating strings? For example, what does ${0%/*}mean?

操作字符串时,bash 中的百分号是什么意思?例如,是什么${0%/*}意思?

采纳答案by Ian Stapleton Cordasco

If you use @fedorqui's resource, you'll see it is going to strip the shortest match of /*from the end of the first positional argument. An example:

如果您使用@fedorqui 的资源,您会看到它将/*从第一个位置参数的末尾剥离最短的匹配项。一个例子:

example_foo(){
    echo ${1%/*}
}

example_foo path/to/directory/sub_directory
# => path/to/directory

In the example I used the second positional argument since the first is the name of the function.

在示例中,我使用了第二个位置参数,因为第一个是函数的名称。