Skip to main content

bash

CommandDescription
TabComplete command
Ctrl+DExit
Ctrl+LClear screen
Ctrl+RSearch history
!!Previous command
!<string>Most recent command starting with <string>

Parameter

ParameterDescription
$#Count of parameter
$0The path of shell script
basename $0The file name of shell script
$1 $2 $3 ...Positional parameter
$* $@$1 $2 $3...
"$*""$1$IFS$2$IFS$3..."
"$@""$1" "$2" "$3"...

Redirect output

File descriptorDescription
0stdin
1stdout
2stderr

Redirect output to file

Defaultstdout
1stdout
2stderr
&stdout and stderr
<command> [1|2|&]> <file>

Redirect stderr to stdout

<command> 2>&1

Pipeline

Pipe stdoutstdin

<command> | <command>

Pipe stdout and stderrstdin

<command> |& <command>

Loop / Iterate

For each file

for <file variable name> in *; do
<...>
done

For each find result

Ref: Stack Overflow

Ref: Stack Overflow

find [<expression>] -print0 | while IFS= read -r -d $'\0' <file variable name>; do
<...>
done

For each array value

array=(a b c)
for i in "${array[@]}"; do
// $i == <current value>
done

Command substitution

$(<command>)
`<command>`

Process substitution

Redirect input to <input command>

<command> <( <input command> )

Redirect output to <output command>

<command> >( <output command> )

cat file

$(< <file>)
$(cat <file>)

Environment variable

Set variable

<name>=<value>

Get variable

$<name>
${<name>}

List variable

env

Export variable

Export env variable to child process

export <name>[=<value>]

History

Search history

Ctrl + R

Remove current session bash history

Clear current history buffer

history -c

Don't save the history when session close

unset HISTFILE

Kill bash

## $$ == Current shell PID
kill -9 $$

Delete all bash history

Empty history file

> $HISTFILE

Set history size to 0

HISTSIZE=0