我对Bash中方括号、圆括号、花括号的用法以及它们的双形式和单形式之间的区别感到困惑。有明确的解释吗?
当前回答
括号、圆括号和大括号的一些常见而方便的用法
如上所述,有时您希望在不丢失返回值的情况下显示消息。这是一个方便的片段:
$ [ -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;
}
其他回答
test,[和[]之间的区别在BashFAQ中有详细的解释。 (注:链接中有很多例子供比较)
To cut a long story short: test implements the old, portable syntax of the command. In almost all shells (the oldest Bourne shells are the exception), [ is a synonym for test (but requires a final argument of ]). Although all modern shells have built-in implementations of [, there usually still is an external executable of that name, e.g. /bin/[. [[ is a new, improved version of it, and it is a keyword, not a program. This has beneficial effects on the ease of use, as shown below. [[ is understood by KornShell and BASH (e.g. 2.03), but not by the older POSIX or BourneShell.
结论是:
什么时候使用新的测试命令[[,什么时候使用旧的测试命令[? 如果要考虑到POSIX或BourneShell的可移植性/一致性,则应该考虑旧语法 被使用。如果脚本需要BASH、Zsh或KornShell, 新的语法通常更加灵活。
函数定义中的括号
括号()在函数定义中被使用:
function_name () { command1 ; command2 ; }
这就是在命令形参中也必须转义括号的原因:
$ echo (
bash: syntax error near unexpected token `newline'
$ echo \(
(
$ echo () { command echo The command echo was redefined. ; }
$ echo anything
The command echo was redefined.
括号、圆括号和大括号的一些常见而方便的用法
如上所述,有时您希望在不丢失返回值的情况下显示消息。这是一个方便的片段:
$ [ -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;
}
A single bracket ([) usually actually calls a program named [; man test or man [ for more info. Example: $ VARIABLE=abcdef $ if [ $VARIABLE == abcdef ] ; then echo yes ; else echo no ; fi yes The double bracket ([[) does the same thing (basically) as a single bracket, but is a bash builtin. $ VARIABLE=abcdef $ if [[ $VARIABLE == 123456 ]] ; then echo yes ; else echo no ; fi no Parentheses (()) are used to create a subshell. For example: $ pwd /home/user $ (cd /tmp; pwd) /tmp $ pwd /home/user As you can see, the subshell allowed you to perform operations without affecting the environment of the current shell. (a) Braces ({}) are used to unambiguously identify variables. Example: $ VARIABLE=abcdef $ echo Variable: $VARIABLE Variable: abcdef $ echo Variable: $VARIABLE123456 Variable: $ echo Variable: ${VARIABLE}123456 Variable: abcdef123456 (b) Braces are also used to execute a sequence of commands in the current shell context, e.g. $ { date; top -b -n1 | head ; } >logfile # 'date' and 'top' output are concatenated, # could be useful sometimes to hunt for a top loader ) $ { date; make 2>&1; date; } | tee logfile # now we can calculate the duration of a build from the logfile
尽管()有一个微妙的语法差异(参见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