This is an old revision of the document!
command &>>/dev/null: Redirect stdout and stderr to /dev/null&1 is referenced to stdout&2 is referenced to stderrcommand 1>>/dev/null: stdout go to /dev/nullcommand 2>>/dev/null: Redirect stderr to /dev/nullcommand 2>>&1: Redirect stderr to stdoutif [[ -f filename ]]; then [...] else [...] fi
-f is a regular file-d is a directory-x file: execute permission-a file: file exists-r file: have read permission-w file: have write permission-s file: file exists and is not empty-nt file2: file1 is newer than file2-ot file2: file1 is older than file2if test $? = 0; then echo 'do this'; else echo 'do that'; fi
if [[ -z $? ]]; then # last command run successfully echo "status code is 0" else echo "status code is not null, something wrong" fi [ -f ~/dotfiles/.bashrc ] && source ~/dotfiles/.bashrc # or [ -z "$ENV_VAR" ] && echo "ENV_VAR is empty" [[ ! -z "$ENV_VAR" ]] && echo "ENV_VAR is not empty" || echo "ENV_VAR is empty"
-z string1 # string is null-n string1 # string is not nullstring1 = string2 # string1 matches string2string1 != string2 # string1 does not match string2string1 == string2 # string1 is equal to string2string1 !== string2 # string1 is not equal to string2-lt: Less than-gt: Greater than-le: Less than or equal to-ge: Greater than or equal to-eq: Equal to-ne: Not equal toif !type brew &>/dev/null; then
echo "brew install"
fi
# or
if which brew &>/dev/null; then
echo 'brew installed'
fi
# or
if command -v brew &> /dev/null; then
echo "brew install"
fi
${parameter-default}, ${parameter:-default}
${parameter=default}, ${parameter:=default}
${parameter+alt_value}, ${parameter:+alt_value}
${parameter?err_msg}, ${parameter:?err_msg}
for name [in list] do [statements that can use $name] done
case expression in
pattern1 )
statements ;;
pattern2 )
statements ;;
esac
The backticks/gravemarks have been deprecated in favor of $() for command substitution.
$* Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable.$@ Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word.$# Expands to the number of positional parameters in decimal.$? Expands to the exit status of the most recently executed foreground pipeline.$- A hyphen expands to the current option flags as specified upon invocation, by the set built-in command, or those set by the shell itself (such as the -i).$$ Expands to the process ID of the shell.$! Expands to the process ID of the most recently executed background (asynchronous) command.$0 Expands to the name of the shell or shell script.$_ The underscore variable is set at shell startup and contains the absolute file name of the shell or script being executed as passed in the argument list. Subsequently, it expands to the last argument to the previous command, after expansion. It is also set to the full pathname of each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file.The implementation of$*has always been a problem and realistically should have been replaced with the behavior of$@. In almost every case where coders use$*, they mean$@.$*Can cause bugs and even security holes in your software.
find . -iname "*filename*" find . -type d