Site Tools


This is an old revision of the document!


bash cheat sheet

๐Ÿ’ป IO Redirect

  • command &>>/dev/null: Redirect stdout and stderr to /dev/null
  • &1 is referenced to stdout
  • &2 is referenced to stderr
  • command 1>>/dev/null: stdout go to /dev/null
  • command 2>>/dev/null: Redirect stderr to /dev/null
  • command 2>>&1: Redirect stderr to stdout

๐Ÿ’ป File condition tests

if [[ -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
  • file1 -nt file2: file1 is newer than file2
  • file1 -ot file2: file1 is older than file2

๐Ÿ’ป check last command status code

if test $? = 0; then
  echo 'do this';
else
  echo 'do that';
fi

๐Ÿ’ป Variable condition tests

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 null
  • string1 = string2 # string1 matches string2
  • string1 != string2 # string1 does not match string2
  • string1 == string2 # string1 is equal to string2
  • string1 !== string2 # string1 is not equal to string2

๐Ÿ’ป numberic conditionals

  • -lt: Less than
  • -gt: Greater than
  • -le: Less than or equal to
  • -ge: Greater than or equal to
  • -eq: Equal to
  • -ne: Not equal to

๐Ÿ’ป check if command available

if !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

๐Ÿ’ป Manipulating and/or expanding variables

If parameter not set, use default
${parameter-default}, ${parameter:-default}
If parameter not set, set it to default
${parameter=default}, ${parameter:=default}
If parameter set, use alt_value, else use null string
${parameter+alt_value}, ${parameter:+alt_value}
If parameter set, use it, else print err_msg and abort the script with an exit status of 1
${parameter?err_msg}, ${parameter:?err_msg}

Shortcuts

  • CTRL L = Clear the screen
  • CTRL A = Cursor to start of line
  • CTRL E = Cursor the end of line
  • CTRL W = Delete word on the left
  • CTRL U = Delete left of the cursor tail = from the bottom
  • CTRL K = Delete right of the cursor -n <#oflines> <fileName>
  • CTRL Y = Paste (after CTRL U,K or W) mkdir = create new folder
  • CTRL R = reverse search history mkdir myStuff/pictures/ ..
  • !! = repeat last command

For

for name [in list]
do
  [statements that can use $name]
done

Case

case expression in
  pattern1 )
    statements ;;
  pattern2 )
    statements ;;
esac

$() vs backtick

ref

The backticks/gravemarks have been deprecated in favor of $() for command substitution.

Special bash variables

ref

  • $* 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
bash/cheat.1742553338.txt.gz ยท Last modified: by dcai