bash
| Command | Description |
|---|---|
Tab | Complete command |
| Ctrl+D | Exit |
| Ctrl+L | Clear screen |
| Ctrl+R | Search history |
!! | Previous command |
!<string> | Most recent command starting with <string> |
Parameter
| Parameter | Description |
|---|---|
$# | Count of parameter |
$0 | The path of shell script |
basename $0 | The file name of shell script |
$1 $2 $3 ... | Positional parameter |
$* $@ | $1 $2 $3... |
"$*" | "$1$IFS$2$IFS$3..." |
"$@" | "$1" "$2" "$3"... |
Redirect output
| File descriptor | Description |
|---|---|
0 | stdin |
1 | stdout |
2 | stderr |
Redirect output to file
| Default | stdout |
1 | stdout |
2 | stderr |
& | stdout and stderr |
<command> [1|2|&]> <file>
Redirect stderr to stdout
<command> 2>&1
Pipeline
Pipe stdout ➡ stdin
<command> | <command>
Pipe stdout and stderr ➡ stdin
<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 + RRemove 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