bash
shorthand
Shorthand | Description |
---|---|
$$ | 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) |
$0 | Name of the script itself |
$1 | First argument |
$2 | Second argument |
$3 | Third 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 |
!foo | Last command beginning with “foo” |
!?baz | Last command containing “baz” |
^foo^bar | Last command with the first occurrence of “foo” replaced with “bar” |
!:gs/foo/bar | Last command with all occurrences of “foo” replaced with “bar” |
<any_above>:p | Prints 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
-
Change shell attributes
-
Resources
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 locatedls $(dirname $(which java))
By default, /bin/sh
points to bash
sh
doesn't havebash
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
-
man test
or
man [
-
Resources
file test operators
Operator | Description |
---|---|
-e | file exists |
-f | file exists and is a regular file |
-d | file exists and is a directory |
-s | file exists and is not empty |
-r | file exists and is readable |
-w | file exists and is writable |
-x | file exists and is executable |
-L | file exists and is a symbolic link |
-O | file exists and is owned by the user |
-G | file exists and is owned by the group |
-nt | file1 is newer than file2 |
-ot | file1 is older than file2 |
-ef | file1 and file2 are hard links to the same file |
integer comparison
Operator | Description |
---|---|
-eq | equal |
-ne | not equal |
-gt | greater than |
-ge | greater than or equal |
-lt | less than |
-le | less than or equal |
string comparison
Operator | Description |
---|---|
= | equal |
!= | not equal |
< | less than, in ASCII alphabetical order |
> | greater than, in ASCII alphabetical order |
-z | string is null, that is, has zero length |
-n | string is not null |
compound comparison
Operator | Description |
---|---|
! | logical not |
-a | logical and, e.g. expr1 -a expr2 |
-o | logical or, e.g. expr1 -o expr2 |
readline
-
Notes
-
readline
is the library handlingbash
CLI interaction. -
Editing mode
can be eitheremacs
orvi
- Emacs:
set -o emacs
- Vi:
set -o vi
- Emacs:
-
Refer to
man readline
for more details. -
User configuration file:
~/.inputrc
-
Global configuration file:
/etc/inputrc
-
-
Resources
readline - Editing Commands
Searching (Emacs)
Command | Description |
---|---|
Ctrl + r | backward command history search |
Ctrl + s | forward command history search |
Ctrl + g | Exit the command history search and clear the input |
Ctrl + p | Retrieve the previous command (same as Up arrow key) |
Ctrl + n | Retrieve 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)
Command | Description |
---|---|
Ctrl + a | Move the cursor to the start of the line |
Ctrl + e | Move the cursor to the end of the line |
Ctrl + f | Move the cursor forward one character (same as <RIGHT ARROW> key) |
Ctrl + b | Move the cursor backward one character (same as <LEFT ARROW> key) |
Meta + f | Move the cursor forward one word (same as Ctrl + <RIGHT ARROW> key) |
Meta + b | Move the cursor backward one word (same as Ctrl + <LEFT ARROW> key) |
Ctrl + x | Move 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)
Command | Description |
---|---|
Ctrl + d / <DELETE> | Delete the character under the cursor |
Ctrl + h / <BACKSPACE> | Delete the character before the cursor |
Ctrl + k | Clear the line content after the cursor, and copies the content to the clipboard |
Ctrl + u | Clear the line content before the cursor, and copies the content to the clipboard |
Ctrl + w | Delete the word before the cursor (backward), and copies the content to the clipboard |
Ctrl + y | Paste the last deleted text |
Ctrl + j / Ctrl + m | Execute 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 U | Convert the text to uppercase from the character under cursor to the end of the word |
Meta and then L | Convert the text to lowercase from the character under cursor to the end of the word |
Meta and then C | Convert the character under cursor to uppercase |
Ctrl + x + e | Invoke an editor (defined by $EDITOR ) to edit the current command line |