Site Tools


bash:cheat

bash cheat sheet

Test last command ret

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

Special bash variables

ref

Character Definition
$* 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.
[email protected] 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 "[email protected]". In almost every case where coders use "$*", they mean "[email protected]". "$*" Can cause bugs and even security holes in your software.

Manipulating and/or expanding variables

more

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

Shortcuts

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

IO Redirect

'&1' is stdout '&2' is stderr

  • '2>/dev/null': Redirect stderr
  • '2>&1': Redirect to stderr to stdout
  • &>/dev/null: Both stdout and stderr go to /dev/null

File condition tests

if [[ -a filename ]]; then
  [...]
else
  [...]
fi
  • -a file: file exists
  • -d file: file exists and is a directory
  • -f file: file exists and is a regular file
  • -r file: have read permission
  • -w file: have write permission
  • -x file: execute 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

Condition tests

if [[ -z $ENV_VAR ]]; then
  [...]
else
  [...]
fi
  • 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
  • -n string1 # string is not null
  • -z string1 # string is null
  • 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

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.

bash/cheat.txt · Last modified: 2020-04-20 by dcai