bash_cheatsheet
This is an old revision of the document!
Table of Contents
Bash Cheatsheet
Special parameters
$*
: Positional parameters. Separated by IFS. | $1c$2c$3$@
: Positional parameters. Separated by space. | $1 $2 $3“$@”
: Positional parameters. Separated by space. Separated words. | “$1” “$2” “$3”$#
: Number of positional parameters.$?
: Exit status of the most recently executed foreground pipeline.$-
: Current option flags as specified upon invocation, by the set builtin command, or those set by the shell itself (such as the -i option).$$
: Process ID of the shell. In a () subshell, it expands to the process ID of the invoking shell, not the subshell.$!
: Process ID of the most recently executed background (asynchronous) command.$0
: Name of the shell or shell script.$_
: Last argument to the previous command, after expansion. (Or absolute shell pathname if first command).
Parameters expansion
- Simple usage
$PARAMETER
${PARAMETER}
- Indirection
${!PARAMETER}
- Case modification
${PARAMETER^}
${PARAMETER^^}
${PARAMETER,}
${PARAMETER,,}
${PARAMETER~}
${PARAMETER~~}
- Variable name expansion
${!PREFIX*}
${!PREFIX@}
- Substring removal (also for filename manipulation!)
${PARAMETER#PATTERN}
${PARAMETER##PATTERN}
${PARAMETER%PATTERN}
${PARAMETER%%PATTERN}
- Search and replace
${PARAMETER/PATTERN/STRING}
${PARAMETER//PATTERN/STRING}
${PARAMETER/PATTERN}
${PARAMETER//PATTERN}
- String length
${#PARAMETER}
- Substring expansion
${PARAMETER:OFFSET}
${PARAMETER:OFFSET:LENGTH}
- Use a default value
${PARAMETER:-WORD}
${PARAMETER-WORD}
- Assign a default value
${PARAMETER:=WORD}
${PARAMETER=WORD}
- Use an alternate value
${PARAMETER:+WORD}
${PARAMETER+WORD}
- Display error if null or unset
${PARAMETER:?WORD}
${PARAMETER?WORD}
Mastering history
!!
: expands to the last command and all arguments!-3
: 3rd-to-last command and all arguments!^
: first argument of the last command in history!:2
: 2nd argument of the last command!$
: last argument of the last command!*
: all arguments of the last command, but not the command itself!42
: expands to the 42nd command in the history list!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
bash_cheatsheet.1401183296.txt.gz · Last modified: 2014/05/27 11:34 by ginko