Bash指令速查

本文总结Linux bash的常见用法,作为Cheat sheet使用。

命令行参数

第一行!#/bin/bash为释伴(shebang)

#!/bin/bash
cp $1 $2
echo "Copy done"
  • $0为文件名
  • $1-$9为参数
  • $#为参数量
  • $$当前bash进程ID
  • $USER
  • $HOSTNAME
  • $PATH,用:间隔

变量名

  • 不加引号:只处理第一个单词
  • 单引号:字面量,同Pythonr'...'
  • 双引号:会根据变量进行替换
#!/bin/bash
dir=../
ls $dir

var=Hello World
# -bash: World: command not found
var='Hello World'
echo $var # Hello World
newvar='More $var'
echo $newvar # More $var
newvar="More $var"
echo $newvar # More Hello World

因此如果要在多条指令中传多个同样的参数,那么可以用单引号括起来声明变量后传入。

命令替换

$(...)进行书写,括号里内容会直接执行

myvar=$( ls /etc )
echo $myvar
# Documents Desktop ...

变量导出

如果变量需要在另外的bash文件中用,则需通过export导出

#!/bin/bash
# demonstrate variable scope 1.
var1=blah
export var1
./script2.sh

算术表达式

let <expr>语句或者$((...))

#!/bin/bash
let "a = 5 + 4"
echo $a # 9
let "a = $1 + 30"
echo $a # 30 + first command line argument
b=$(( $a + 3 ))
echo $b

条件分支

#!/bin/bash
# Basic if statement
if [ $1 -gt 100 ]
then
    echo Hey that\'s a large number.
    pwd
fi
date
Operator Description
! EXPRESSION The EXPRESSION is false.
-n STRING The length of STRING is greater than zero.
-z STRING The lengh of STRING is zero (ie it is empty).
STRING1 = STRING2 STRING1 is equal to STRING2
STRING1 != STRING2 STRING1 is not equal to STRING2
INTEGER1 -eq INTEGER2 INTEGER1 is numerically equal to INTEGER2
INTEGER1 -gt INTEGER2 INTEGER1 is numerically greater than INTEGER2
INTEGER1 -lt INTEGER2 INTEGER1 is numerically less than INTEGER2
-d FILE FILE exists and is a directory.
-e FILE FILE exists.
-r FILE FILE exists and the read permission is granted.
-s FILE FILE exists and it’s size is greater than zero (ie. it is not empty).
-w FILE FILE exists and the write permission is granted.
-x FILE FILE exists and the execute permission is granted.
  • and - &&
  • or -  
#!/bin/bash
# case example
case $1 in
    start) # pattern 1
        echo starting
        ;; # needed!
    stop)
        echo stoping
        ;;
    restart)
        echo restarting
        ;;
    *)
        echo don\'t know
        ;;
esac # needed!

循环

#!/bin/bash
# Basic while loop
counter=1
while [ $counter -le 10 ]
do
    echo $counter
    ((counter++))
done

names='Stan Kyle Cartman'
for name in $names
do
    echo $name
done

for value in {1..5}
do
    echo $value
done

echo "All done"

日志

# & means run in backstage
# nohup means not stopping running after the terminal is dead
# > means redirect output to file
$ nohup ./run.sh > out.log &

# show output on the screen and also output to file
$ ./run.sh | tee out.log

# https://stackoverflow.com/questions/6674327/redirect-all-output-to-file-in-bash
# The > operator redirects the output usually to a file but it can be to a device. You can also use >> to append.
# > file redirects stdout to file
# 1> file redirects stdout to file
# 2> file redirects stderr to file
# &> file redirects stdout and stderr to file
# /dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output.
$ ./run.sh > out.log 2>&1

# run the program for 10s
$ timeout 10 top

# list all programs
ps -fu username

参考资料