[英]I don't understand this parameter expansion: ${p//[0-9]/}
In Linux /etc/init.d/functions
script I found the following parameter expansions that I don't quite understand: 在Linux
/etc/init.d/functions
脚本中,我发现了以下参数扩展,我不太明白:
${p//[0-9]/} replace all instances of any number to/by what?
${1##[-+]} This seems to remove all the longest left instances of minuses and pluses?
${LSB:-} This seems to say that if LSB is not set then set nothing? in other words do nothing?
These are instances of bash
Shell Parameter Expansion ; 这些是
bash
Shell参数扩展的实例; see http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html 请参阅http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
Note: ksh
and zsh
support the expansions in your question, too (I'm unclear on the full extent of the overlap in functionality), whereas sh
(POSIX-features-only shells), does NOT support the string-replacement expansion, ${p//[0-9]/}
. 注意:
ksh
和zsh
支持你问题中的扩展(我不清楚功能重叠的全部范围),而sh
(POSIX-features-only shell)不支持字符串替换扩展, ${p//[0-9]/}
。
${p//[0-9]/}
Removes all digits : replaces all ( //
) instances of digits ( [0-9]
) with an empty string - ie, it removes all digits (what comes after the last /
is the replacement string, which is empty in this case). 删除所有数字 :用空字符串替换所有(
//
)数字实例( [0-9]
) - 即删除所有数字(在最后一个/
是替换字符串,在这种情况下为空) 。
${1##[-+]}
Strips a single leading -
or +
, if present : Technically, this removes the longest prefix ( ##
) composed of a single -
or +
character from parameter $1
. 条单个主导
-
或+
,如果存在的话 :从技术上讲,这消除最长前缀( ##
单个组成) -
或+
从参数字符$1
。 Given that the search pattern matches just a single character, there is no need to use ##
for the longest prefix here, and #
- for the shortest prefix - would do. 鉴于搜索模式只是一个单一的字符相匹配,就没有必要使用
##
这里最长前缀, #
-在最短的前缀-会做。
${LSB:-}
A no-op designed to prevent the script from breaking when run with the -u
( nounset
) shell attribute : Technically, this expansion means: In case variable $LSB
is either not set or empty , it is to be replaced with the string following :-
, which, in this case, is also empty. 设计用于防止脚本在使用
-u
( nounset
)shell属性运行时破坏的无操作 :从技术上讲,此扩展意味着:如果变量$LSB
未设置或为空 ,则将替换为以下字符串:-
,在这种情况下,也是空的。
While this may seem pointless at first glance, it has its purpose, as Sigi points out: 虽然乍一看这似乎毫无意义,但它有其目的,正如Sigi所指出的那样:
" “
The ${LSB:-}
construct makes perfect sense if the shell is invoked with the -u
option (or set -u
is used), and the variable $LSB
might actually be unset. 如果使用
-u
选项调用shell(或使用set -u
),则${LSB:-}
构造非常有意义,并且实际上可能未设置变量$LSB
。 You then avoid the shell bailing out if you reference $LSB
as ${LSB:-}
instead. 如果您将
$LSB
引用为${LSB:-}
则可以避免shell退出。 Since it's good practice to use set -u
in complex scripts, this move comes in handy quite often. 由于在复杂脚本中使用set
-u
是一种很好的做法,因此这一举措很常见。
" “
${p//[0-9]/} # removes digits from anywhere in `$p`
${1##[-+]} # removes + or - from start in $1
${LSB:-} # not really doing anything
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.