经常我们写脚本的时候是直接
#!/bin/bash
这样写,但是运行的时候是直接sh xx.sh这样运行的。细心一点会发现有的脚本使用bash xx.sh能允许但是不能使用sh xxx.sh允许。比如shellhacks里面的
1.6.1. Logging and Monitoring with Tee Sometimes you want to capture all standard output to a log file while monitoring the output yourself. We can use the exec I/O redirection to do this also along with process substitution: exec > >(tee -a ${0##*/}.log) If you wanted to redirect stderr to a different file: exec 2> >(tee -a ${0##*/}.err)
这是我们可以在脚本开头set -o 打印出两种运行情况的区别。sh xx.sh的时候是开启了posix兼容,而禁用了bash的一些基本特性。
可以在脚本开头set +o posix,强制关闭posix兼容。这样在两种情况下脚本允许的结果都是相同的。不过我在debian下测试有点问题,主要是debian下的sh实际是链接到dash去了。