Shell

bash

shorthand

ShorthandDescription
$$PID
$!PID of the last background command
$?Return code of the last statement
$#Number of arguments passed to the script
$*All positional arguments (as a single word)
$@All positional arguments (as separate strings)
$0Name of the script itself
$1First argument
$2Second argument
$3Third argument
!^First argument of the last command
!!Last command and all arguments
!:<N>Nth argument of the last command
!$Last argument of the last command
!*All arguments of the last command
!fooLast command beginning with “foo”
!?bazLast command containing “baz”
^foo^barLast command with the first occurrence of “foo” replaced with “bar”
!:gs/foo/barLast command with all occurrences of “foo” replaced with “bar”
<any_above>:pPrints command without executing

Built-in command

man bash-builtins

bash builtin - alias

Temporarily unalias a command
\$alias

bash builtin - export

  • A script can export variables only to child processes, that is, only to commands or processes which that particular script initiates. A script invoked from the command-line cannot export variables back to the command-line environment. Child processes cannot export variables back to the parent processes that spawned them.

bash builtin - which

Get all occurrences of a program under the PATH
which -a $executable

bash builtin - declare

Get a function definition
declare -f $function_name

bash builtin - set

set - Print script statements as they are executed
  • set -x

    Will print actual arguments passed to a function

bash builtin - shopt

bash builtin - unset

Remove an environment variable
unset $variable
Get the status of all options
shopt | column -t
Enable switching directory without using cd command
shopt -s autocd

Cheatsheet - bash

Command substitution

  • Get contents of the directory where java is located

    ls $(dirname $(which java))

By default, /bin/sh points to bash

  • sh doesn't have bash specific features.

Run a command with bash while outside bash

  • bash -c "<COMMAND>"

    eg: bash -c "ls -al"

Print commands and arguments as they are executed

PS4='+$BASH_SOURCE> ' bash -xl > bash.log 2>&1

Condition evalluation

file test operators

OperatorDescription
-efile exists
-ffile exists and is a regular file
-dfile exists and is a directory
-sfile exists and is not empty
-rfile exists and is readable
-wfile exists and is writable
-xfile exists and is executable
-Lfile exists and is a symbolic link
-Ofile exists and is owned by the user
-Gfile exists and is owned by the group
-ntfile1 is newer than file2
-otfile1 is older than file2
-effile1 and file2 are hard links to the same file

integer comparison

OperatorDescription
-eqequal
-nenot equal
-gtgreater than
-gegreater than or equal
-ltless than
-leless than or equal

string comparison

OperatorDescription
=equal
!=not equal
<less than, in ASCII alphabetical order
>greater than, in ASCII alphabetical order
-zstring is null, that is, has zero length
-nstring is not null

compound comparison

OperatorDescription
!logical not
-alogical and, e.g. expr1 -a expr2
-ological or, e.g. expr1 -o expr2

readline

readline - Editing Commands

Searching (Emacs)
CommandDescription
Ctrl + rbackward command history search
Ctrl + sforward command history search
Ctrl + gExit the command history search and clear the input
Ctrl + pRetrieve the previous command (same as Up arrow key)
Ctrl + nRetrieve the next command (same as Down arrow key)
  • Once command is found, use any movement command to exit search.
  • Typing #tag after the command can be used to tag a command for future reference. #tag is a comment from shell perspective.
Moving (Emacs)
CommandDescription
Ctrl + aMove the cursor to the start of the line
Ctrl + eMove the cursor to the end of the line
Ctrl + fMove the cursor forward one character (same as <RIGHT ARROW> key)
Ctrl + bMove the cursor backward one character (same as <LEFT ARROW> key)
Meta + fMove the cursor forward one word (same as Ctrl + <RIGHT ARROW> key)
Meta + bMove the cursor backward one word (same as Ctrl + <LEFT ARROW> key)
Ctrl + xMove between the beginning of the line and the current position of the cursor. This allows you to press Ctrl + x to return to the start of the line, change something, and then press Ctrl + x to go back to your original cursor position. To use this shortcut, hold the Ctrl key and tap the x key twice.
Editing (Emacs)
CommandDescription
Ctrl + d / <DELETE>Delete the character under the cursor
Ctrl + h / <BACKSPACE>Delete the character before the cursor
Ctrl + kClear the line content after the cursor, and copies the content to the clipboard
Ctrl + uClear the line content before the cursor, and copies the content to the clipboard
Ctrl + wDelete the word before the cursor (backward), and copies the content to the clipboard
Ctrl + yPaste the last deleted text
Ctrl + j / Ctrl + mExecute the command
Meta + d / Ctrl + <DELETE>Delete the word after the cursor (forward)
Meta + <BACKSPACE>Delete the word before the cursor (backward)
Meta and then UConvert the text to uppercase from the character under cursor to the end of the word
Meta and then LConvert the text to lowercase from the character under cursor to the end of the word
Meta and then CConvert the character under cursor to uppercase
Ctrl + x + eInvoke an editor (defined by $EDITOR) to edit the current command line