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}: Value of the variable whose name isPARAMETER.
- Case modification
${PARAMETER^}: First upper.${PARAMETER^^}: Whole upper.${PARAMETER,}: First lower.${PARAMETER,,}: Whole lower.${PARAMETER~}: Switch first.${PARAMETER~~}: Switch all.
- Variable name expansion
${!PREFIX*}: List of all variables and arrays names beginning withPREFIX.${!PREFIX@}: Idem.
- Substring removal (also for filename manipulation!)
${PARAMETER#PATTERN}: Strip left (short match).${PARAMETER##PATTERN}: Strip left (long match).${PARAMETER%PATTERN}: Strip right (short match).${PARAMETER%%PATTERN}: Strip right (long match).
- Search and replace
${PARAMETER/PATTERN/STRING}: Replace (fisrt occurence).${PARAMETER//PATTERN/STRING}: Replace (all occurences).${PARAMETER/PATTERN}: Search (first occurence).${PARAMETER//PATTERN}Search (all occurrences).
- String length
${#PARAMETER}
- Substring expansion
${PARAMETER:OFFSET}${PARAMETER:OFFSET:LENGTH}
- Use a default value
${PARAMETER:-WORD}:WORDwhenPARAMETERis unset or empty.${PARAMETER-WORD}:WORDwhenPARAMETERis unset.
- Assign a default value
${PARAMETER:=WORD}: Expand AND assignWORDwhenPARAMETERis unset or empty.${PARAMETER=WORD}: Expand AND assignWORDwhenPARAMETERis unset.
- Use an alternate value
${PARAMETER:+WORD}: Nothing ifPARAMETERis unset or empty,WORDelse.${PARAMETER+WORD}: Nothing ifPARAMETERis unset,WORDelse.
- Display error if null or unset
${PARAMETER:?WORD}: Like:-AND sets non-null exit code and$?.${PARAMETER?WORD}: Like-AND sets non-null exit code and$?.
Work with file paths
dirname=${fullpath%/*}filename=${fullpath##*/}extension=${filename##*.}~% FILE="example.tar.gz" ~% echo "${FILE%%.*}" example ~% echo "${FILE%.*}" example.tar ~% echo "${FILE#*.}" tar.gz ~% echo "${FILE##*.}" gz
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
Redirection
- Diriger plusieurs lignes vers un fichier :
/bin/cat <<EOM >$FILE text1 text2 text3 text4 EOM
Fonctions
func_name () { cmd1; cmd2 }
On retrouve cette syntax au coeur de la fameuse fork bomb bask :(){ :|:& };: où : est le nom de la fonction. L'utilisation du pipe et des : est a priori seulement à pour obfusquer un peu plus la syntaxe (:) et la raccourcir (|). f(){f&;f&;}f fait quaisment la même chose.
bash_cheatsheet.1417977004.txt.gz · Last modified: 2014/12/07 19:30 by ginko
