我对Bash中方括号、圆括号、花括号的用法以及它们的双形式和单形式之间的区别感到困惑。有明确的解释吗?
当前回答
括号
if [ CONDITION ] Test construct
if [[ CONDITION ]] Extended test construct
Array[1]=element1 Array initialization
[a-z] Range of characters within a Regular Expression
$[ expression ] A non-standard & obsolete version of $(( expression )) [1]
[1] http://wiki.bash-hackers.org/scripting/obsolete
花括号
${variable} Parameter substitution
${!variable} Indirect variable reference
{ command1; command2; . . . commandN; } Block of code
{string1,string2,string3,...} Brace expansion
{a..z} Extended brace expansion
{} Text replacement, after find and xargs
括号
( command1; command2 ) Command group executed within a subshell
Array=(element1 element2 element3) Array initialization
result=$(COMMAND) Command substitution, new style
>(COMMAND) Process substitution
<(COMMAND) Process substitution
双括号
(( var = 78 )) Integer arithmetic
var=$(( 20 + 5 )) Integer arithmetic, with variable assignment
(( var++ )) C-style variable increment
(( var-- )) C-style variable decrement
(( var0 = var1<98?9:21 )) C-style ternary operation
其他回答
括号
if [ CONDITION ] Test construct
if [[ CONDITION ]] Extended test construct
Array[1]=element1 Array initialization
[a-z] Range of characters within a Regular Expression
$[ expression ] A non-standard & obsolete version of $(( expression )) [1]
[1] http://wiki.bash-hackers.org/scripting/obsolete
花括号
${variable} Parameter substitution
${!variable} Indirect variable reference
{ command1; command2; . . . commandN; } Block of code
{string1,string2,string3,...} Brace expansion
{a..z} Extended brace expansion
{} Text replacement, after find and xargs
括号
( command1; command2 ) Command group executed within a subshell
Array=(element1 element2 element3) Array initialization
result=$(COMMAND) Command substitution, new style
>(COMMAND) Process substitution
<(COMMAND) Process substitution
双括号
(( var = 78 )) Integer arithmetic
var=$(( 20 + 5 )) Integer arithmetic, with variable assignment
(( var++ )) C-style variable increment
(( var-- )) C-style variable decrement
(( var0 = var1<98?9:21 )) C-style ternary operation
关于如何使用括号对表达式进行分组和展开的其他信息: (它列在链接语法括号中)
这里有一些要点:
在子shell中分组命令:() (列表)
将当前shell中的命令分组:{} {列表;}
Test -返回一个表达式的二进制结果:[[]] [表达式]]
算术扩张 算术展开的格式是: $(表达式)
简单算术求值的格式是: (表达)
组合多个表达式 (表达式) (expr1 && expr2))
括号、圆括号和大括号的一些常见而方便的用法
如上所述,有时您希望在不丢失返回值的情况下显示消息。这是一个方便的片段:
$ [ -f go.mod ] || { echo 'File not found' && false; }
这不会产生输出,如果文件运行,返回值为0 (true)。Mod在当前目录中存在。测试结果:
$ echo $?
0
如果文件不存在,你会得到消息,但返回值也为1 (false),这也可以测试:
$ [ -f fake_file ] || { echo 'File not found'; false; }
File not found
$ echo $?
1
你也可以简单地创建一个函数来检查文件是否存在:
fileexists() { [ -f "$1" ]; }
或者如果文件是可读的(未损坏,有权限等):
canread() { [ -r "$1" ]; }
如果它是一个目录:
isdir() { [ -d "$1" ]; }
或当前用户可写:
canwrite() { [ -w "$1" ]; }
或者如果一个文件存在并且不是空的(就像一个包含内容的日志文件…)
isempty() { [ -s "$1" ]; }
有更多的细节:TLDP
你还可以查看一个程序是否存在并且在路径上可用:
exists () { command -v $1 > /dev/null 2>&1; }
这在脚本中很有用,例如:
# gitit does an autosave commit to the current
# if Git is installed and available.
# If git is not available, it will use brew
# (on macOS) to install it.
#
# The first argument passed, if any, is used as
# the commit message; otherwise the default is used.
gitit() {
$(exists git) && {
git add --all;
git commit -m "${1:-'GitBot: dev progress autosave'}";
git push;
} || brew install git;
}
Truncate the contents of a variable
$ var="abcde"; echo ${var%d*}
abc
Make substitutions similar to sed
$ var="abcde"; echo ${var/de/12}
abc12
Use a default value
$ default="hello"; unset var; echo ${var:-$default}
hello
在Bash中,test和[是shell内置的。
双括号是shell关键字,它支持额外的功能。例如,您可以使用&&和||来代替-a和-o,并且有一个正则表达式匹配操作符=~。
此外,在一个简单的测试中,双方括号的计算速度似乎比单方括号快得多。
$ time for ((i=0; i<10000000; i++)); do [[ "$i" = 1000 ]]; done
real 0m24.548s
user 0m24.337s
sys 0m0.036s
$ time for ((i=0; i<10000000; i++)); do [ "$i" = 1000 ]; done
real 0m33.478s
user 0m33.478s
sys 0m0.000s
大括号,除了分隔变量名外,还用于参数展开,这样你就可以做以下事情:
截断变量的内容 $ var = "中的";echo $ {var % d *} 美国广播公司 进行类似sed的替换 $ var = "中的";echo $ {var /德/ 12} abc12 使用默认值 默认美元=“你好”;未设置的var;echo $ {var: - $默认} 你好 还有更多
同样,大括号展开创建字符串列表,这些字符串通常在循环中迭代:
$ echo f{oo,ee,a}d
food feed fad
$ mv error.log{,.OLD}
(error.log is renamed to error.log.OLD because the brace expression
expands to "mv error.log error.log.OLD")
$ for num in {000..2}; do echo "$num"; done
000
001
002
$ echo {00..8..2}
00 02 04 06 08
$ echo {D..T..4}
D H L P T
请注意,前导零和增量特性在Bash 4之前是不可用的。
感谢gboffi提醒我关于大括号展开。
双括号用于算术运算:
((a++))
((meaning = 42))
for ((i=0; i<10; i++))
echo $((a + b + (14 * c)))
它们使您可以省略整数和数组变量上的美元符号,并在操作符周围包含空格以提高可读性。
单括号也用于数组下标:
array[4]="hello"
element=${array[index]}
右边的(most/all?)数组引用需要花括号。
Ephemient的评论提醒了我,括号也用于subshell。它们被用来创建数组。
array=(1 2 3)
echo ${array[1]}
2