每一个命令或是命令序列都是经过分号(;)或换行符来分隔java
$ cmd1 ;cmd2 shell
等价于express
$cmd1ubuntu
$cmd2数组
双引号 --- 变量名替换;特殊字符序转义字符(\) -e才生效bash
单引号 --- 变量名不会被扩展,依照原样输出;特殊字符序转义字符(\) -e才生效app
m11@xubuntu:~$ echo -e "aaa\nbbb" aaa bbb m11@xubuntu:~$ echo -e 'aaa\nbbb' aaa bbb
printf --- 按格式打印字符串 spa
root@dxx-VirtualBox:~# printf "%-5s %-10s %-4.2f\n" $var bbb 1.123456 aaaa bbb 1.12
var=valuecode
var = valuexml
export 变量名 :当前shell脚本执行的任何应用程序都会继承这个变量
${parameter:+expression} : 若是parameter有值且不为空,则赋expression的值
root@xubuntu:/home/m11# aaa="bbb"&&echo ${aaa:+"----"} ---- root@xubuntu:/home/m11# aaa=""&&echo ${aaa:+"----"}
${parameter:=expression} : 若是parameter为空,则赋expression的值;不为空,则使用原值
root@xubuntu:/home/m11# aaa=""&&echo ${aaa:="----"} ---- root@xubuntu:/home/m11# aaa="aaa"&&echo ${aaa:="----"} aaa
计算中变量'$'字符无关紧要。
包裹计算表达式的$(())、和$[]必须有'$'字符。
root@xubuntu:/home/m11# aa=111;bb=222 && let rs=aa*bb && echo $rs 24642 root@xubuntu:/home/m11# aa=111;bb=222 && let rs=$aa*bb && echo $rs 24642 root@xubuntu:/home/m11# aa=111;bb=222 && let rs=[aa*bb] && echo $rs bash: let: rs=[aa*bb]: syntax error: operand expected (error token is "[aa*bb]") root@xubuntu:/home/m1# aa=111;bb=222 && let rs=$[aa*bb] && echo $rs 24642 root@xubuntu:/home/m11# aa=111;bb=222 && let rs=$[$aa*bb] && echo $rs 24642 root@xubuntu:/home/m11# aa=111;bb=222 && let rs=(($aa*bb)) && echo $rs bash: syntax error near unexpected token `(' root@xubuntu:/home/m11# aa=111;bb=222 && let rs=$(($aa*bb)) && echo $rs 24642 root@xubuntu:/home/m11# aa=111;bb=222 && let rs=$((aa*bb)) && echo $rs 24642
echo "4 * 5" |bc echo "scale=2;3/8 " |bc 设置小数精度为2 aaa=10;echo "obase=2;$aaa " |bc 进制转换为二进制 echo "sqrt(100)" |bc 平方根 echo "10^2" |bc 平方
0 stdin
1 stdout
2 stderr
> 只将标准输出进行重定向,等价于 1>
2> 只将错误输出进行重定向
2> xxx 1> xxx1 将错误输出到xxx中,将标准输出到xxx1中
1> test 2>&1 将将标准输出到test中,而且错误输出传递给1输出通道 等价于&> test
tee 将数据重定向到文件中,同时提供数据副本做为后续命令的stdin
cat<<END 将END间的数据都当作数据
#!/bin/bash cat<<END <xml>adsfasdf <h1>asdfasdf </h1> </xml> END
定义、使用、赋值、打印
arry=(1 2 3 4 ) root@xubuntu:/home/m11/test# echo ${arry[2]} 3 root@xubuntu:/home/m11/test# echo ${arry[*]} 1 2 3 4 root@xubuntu:/home/m11/test# echo ${arry[@]} 1 2 3 4 root@xubuntu:/home/m11/test# arry[1]=12 root@xubuntu:/home/m11/test# echo ${arry[@]} 1 12 3 4 root@xubuntu:/home/m11/test# echo ${#arry[*]} 4
root@xubuntu:/home/m11/test# declare -A ass_arry root@xubuntu:/home/m11/test# ass_arry=([name]="dxx" [age]="12") root@xubuntu:/home/m11/test# echo ${ass_arry[name]} dxx root@xubuntu:/home/m11/test# echo ${!ass_arry[*]} name age root@xubuntu:/home/m11/test# echo ${!ass_arry[@]} name age
root@xubuntu:/home/m11/test# alias lh='ls -lh' 定义别名 root@xubuntu:/home/m11/test# unalias lh 删除别名
忽略别名,使用转义字符 '\' ,格式 \ command
pgrep 进程名字 :获取进程的PID
cat /proc/1977/environ :查看1977进程的环境变量
root@dxx-VirtualBox:~# pgrep nm-applet 1977 root@dxx-VirtualBox:~# cat /proc/1977/environ GNOME_KEYRING_PID=USER=dxxLANGUAGE=en_USLC_TIME=zh_CN.UTF-8XDG_SEAT=seat0JAVA_TOOL_OPTIONS=-javaagent:/usr/share/java/jayatanaag.jar XDG_SESSION_TYPE=x11COMPIZ_CONFIG_PROFIL