bashrc

# terminal color
export PS1='[\[\e[01;32m\]\u@\h\[\e[00m\] \[\e[01;34m\]\W\[\e[00m\]]\$ '

重定向

cmd $>output.txt
cmd |tee file1 |cmd

子shell

# cat files.txt |xargs -I {} cat {}
cat files.txt | (while read args; do cat $arg; done)
cmd0 |(cd path;cmd1;cmd2) |cmd3

各种命令

  • seq 0 100返回0到100的整数,分隔符为换行
  • 数值比较用[],字符串比较用[[]]

内建功能

|变量| 作用 | |—-|—————-| | $0 | 脚本名称 | | $@ | “$1” “$2” “$3” | | $* | “$1c$2c$3” | | $? | 上一个命令退出状态 | | IFS| 分隔符集合 |

c表示INS的第一个字符

length=${#var} #获取字符串长度
echo ${array_var[*]} #打印数组所有值
declare -A arr #定义关联数组
echo ${!arr[*]} #列出关联数组索引
$(cd /bin; ls) #子shell比反引用更好

数学运算(整数)

let result=no1+no2
result=$[ no1 + no2]
result=$(( no1 + no2))

数学运算(高级)

result=`echo "$no * 1.5" |bc`
result=`echo "scale=2;22/7" |bc` #设定浮点数精度
result=`echo "obase=G;ibase=A;$no" |bc` #16进制转10进制
result=`echo "scale=2;sqrt(88)`

常用代码块

遍历带空格参数

while test -n "${1}"; do
	" do something
	shift 1
done

{name%.*},%删除右边非贪婪匹配的部分,返回去除后缀的文件名

{name##*.},##删除左边贪婪匹配的部分,返回文件名后缀