What is the essential difference between compound command and normal command in bash? -
i learning bash. learning [[ ... ]] command , (( ... )) command. called compound command, discriminated normal command, [. read article of "compound commands" in bash manual. seems runs own rules. unfortunately, not think of why called "compound". knows it, please let me know. thank much.
bash distinguishes between simple commands , compound commands:
simple commands single command optional arguments , redirections. example:
$ echo hello hello
compound commands combine 1 or more simple commands functions single unit. example:
$ { echo hello; date; } hello sun aug 28 23:16:03 pdt 2016
one useful feature of compound commands redirections applied compound command applied every command contains. example:
$ { echo info1; echo info2; } >logfile $ cat logfile info1 info2
according man bash
, there 4 types of compound commands:
group:
{...;}
, illustrated above can used group simple commands form compound command.subshell:
(...)
similar group except commands run in subshell environment. means variable assignments not survive after subshell completes. example:$ a=0; (a=10; echo "inside=$a"); echo "outside=$a" inside=10 outside=0
arithmetic expression: inside double-parens, series of comma-separated arithmetic calculations may performed. example:
$ ((a=2, a=10*a, a+=2)); echo "a=$a" a=22
test command: bash's advanced form of test command,
[[...]]
, can include several tests. tests separated&&
or||
:$ [[ == b || 3 -gt 2 && 4 -gt 3 ]]; echo $? 0
documentation
from man bash
:
compound commands
a compound command 1 of following. in cases list in command's description may separated rest of command 1 or more newlines, , may followed newline in place of semicolon.
(list)
list executed in subshell environment (see command execution environment below). variable assignments , builtin commands affect shell's environment not remain in effect after command completes. return status exit status of list.
{ list; }
list executed in current shell environment. list must terminated newline or semicolon. known group command. return status exit status of list. note unlike metacharacters ( , ), { , } reserved words , must occur reserved word permitted recognized. since not cause word break, must separated list whitespace or
shell metacharacter.
((expression))
the expression evaluated according rules described below under arithmetic evaluation. if value of expression non-zero, return status 0; otherwise return status 1. equivalent let "expression".
[[ expression ]]
return status of 0 or 1 depending on evaluation of conditional expression expression. expressions composed of primaries described below under conditional expressions. word splitting , pathname expansion not performed on words between [[ , ]]; tilde expansion, parameter , variable expansion, arithmetic expansion, command substitution, process substitution, , quote removal performed. conditional operators such -f must unquoted recognized primaries.
when used [[, < , > operators sort lexicographically using current locale.
Comments
Post a Comment