shell variables {} ()

The prototype 1.Shell variable: $ {var}
Prototype variables: $ {var}, that is, add a brace to define the scope of the variable name
 
[root@bogon sh]# aa='ajax'
[root@bogon sh]# echo $aa
ajax
[root@bogon sh]# echo $aa_AA
 
[root@bogon sh]# echo ${aa}_AA
ajax_AA
 
2.$(cmd)
[root@bogon t]# ls 1.txt 2.txt
[root@bogon t]# echo $(ls)
1.txt 2.txt
echo $ (ls) execution
shell command line scan again found $ (cmd) structure, put $ cmd execution (cmd) in the first, to obtain its standard output,
This command is then output into the original echo $ (LS) in $ (LS) position, i.e., replacing the $ (LS), and then execute the echo command
as follows:
echo $ (ls) is replaced by echo 1.txt 2.txt
Here we must note that the error output $ (cmd) in the command will not be replaced, but the replacement of the standard output
3, a series of command execution ()} and {
 
(), And {} is a string of commands for execution, but differ:
Same point:
(), And {} is a string of commands in brackets, and with between the command; spaced No.
 
difference:
() Is a string of commands to re-open a sub-shell execution, {} in the current chain of commands performed on the shell
 
() Can not last command semicolon, the last command} {semicolons
[Root @ dev opt] # var = test
[Root @ dev opt] # (var = 'notest'; echo $ var;)
notest
[Root @ dev opt] # (var = 'notest'; echo $ var)
notest
[Root @ dev opt] # {var = notest; echo $ var;}
notest
 
There must be a space between the first command and left parenthesis () in the first command and left brackets do not have the space, {the}
[Root @ dev opt] # {var = notest; echo $ var;}
notest
 
()和{}中括号里面的某个命令的重定向只影响该命令,但括号外的重定向则影响到括号里的所有命令
 
从1加到100的和:
[root@dev opt]# cat for.sh
#!/bin/bash
sum=0
for  ((i=1; i<=100; i++))
do
  sum=`expr $sum + $i`
done
echo $sum

Guess you like

Origin www.cnblogs.com/putihuakai/p/11401469.html