Arthur Andersen

Operations on Bash Variables and the Usual Suspects

tilbashvariables

Lately I have been catching myself looking up bash variable operations too often. This is something I want to keep in the back of my head.

The Bash Beginners Guide gives a good overview on Operations on variables.

TL;DR

OperationDescription
${#STR}Length of the value of $STR
${STR:-test}Default $STR to “test”
[ -z "${INT:-}" ] && INT=80Default $INT to value 80
${STR:OFFSET}Keep everything after OFFSET
${STR:OFFSET:LENGTH}Keep LENGTH characters after OFFSET
${STR/PATTERN/STRING}Replace first occurence of STRING
${STR//PATTERN/STRING}Replace all occurences of STRING
${VAR#WORD}Remove first occurence of WORD
${VAR##WORD}Remove all occurences of WORD
${VAR#PATTERN}Remove shortest match of PATTERN
${VAR##PATTERN}Remove longest match of PATTERN
${VAR%WORD}Keep first occurence of WORD
${VAR%%WORD}Keep all occurences of WORD
${VAR%PATTERN}Keep shortest match of PATTERN
${VAR%%PATTERN}Keep longest match of PATTERN

Usual Suspects

OperationDescriptionEffectively
${VAR##*/}Remove path until filenameKeep filename
${VAR%/*}Keep the path without filenameKeep dirname