bash中常用的特殊变量
[ Shell ]

shebang - 在shebang中使用env命令可以提高脚本的可移植性。

test.sh:

#!/usr/bin/env bash

echo "Starting program at $(date)" # Date will be substituted

echo "Running program $0 with $# arguments with pid $$"

for file in "$@"; do
    grep foobar "$file" > /dev/null 2> /dev/null
    # When pattern is not found, grep has exit status 1
    # We redirect STDOUT and STDERR to a null register since we do not care about them
    if [[ $? -ne 0 ]]; then
        echo "File $file does not have any foobar, adding one"
        echo "# foobar" >> "$file"
    fi
done
参考资料:

[1] Advanced Bash-Scripting Guide

[2] MIssing-semester:Shell 工具和脚本