Feature Group
bash internal / data manipulations (105)
bash
This comprehensive snippet demonstrates various parameter expansion techniques in Bash, including default value substitution, conditional assignment, error handling, and advanced string manipulation.
#foo=<value> ${foo:-val} # $foo, or val if unset (or null) ${foo:=val} # Set $foo to val if unset (or null) ${foo:+val} # val if $foo is set (and not null) ${foo:?message} # Show error message and exit if $foo is unset (or null) ${foo%suffix} # Remove suffix ${foo#prefix} # Remove prefix ${foo%%suffix} # Remove long suffix ${foo/%suffix} # Remove long suffix ${foo##prefix} # Remove long prefix ${foo/#prefix} # Remove long prefix ${foo/from/to} # Replace first match ${foo//from/to} # Replace all ${foo/%from/to} # Replace suffix ${foo/#from/to} # Replace prefix
bash internaldata manipulationsstring manipulation and expansionsparameter expansion
bash
This demonstrates the use of brace expansion in Bash to generate sequences of numbers and letters, while noting the limitation of not being able to use variables within brace expansions.
echo {1..10} # => 1 2 3 4 5 6 7 8 9 10 echo {a..z} # => a b c d e f g h i j k l m n o p q r s t u v w x y z # This will output the range from the start value to the end value. # Note that you can't use variables here: from=1 to=10 echo {$from..$to} # => {$from..$to} echo {{1..3},{7..9}} # => 1 2 3 7 8 9
bash internaldata manipulationsstring manipulation and expansionsparameter expansionbrace expansion
bash
This snippet demonstrates file path manipulation using parameter expansion in Bash, including removing file extensions, extracting directory paths, getting file extensions, and replacing substrings.
str="/path/to/foo.cpp" echo "$str" # /path/to/foo (variable expansion) echo "${str}" # /path/to/foo (variable expansion) echo '$str' # '$str' (disabled variable expansion) echo "${str%.cpp}" # /path/to/foo echo "${str%.cpp}.o" # /path/to/foo.o echo "${str%/*}" # /path/to echo "${str##*.}" # cpp (extension) echo "${str##*/}" # foo.cpp (basepath) echo "${str#*/}" # path/to/foo.cpp echo "${str##*/}" # foo.cpp echo "${str/foo/bar}" # /path/to/bar.cpp
bash internaldata manipulationsstring manipulation and expansionsparameter expansionpath manipulation
bash
This demonstrates string substitution using parameter expansion to replace the first occurrence of a substring.
echo "${variable/Some/A}" # => A string # This will substitute the first occurrence of "Some" with "A".
bash internaldata manipulationsstring manipulation and expansionsparameter expansionstring substitution
bash
This demonstrates providing a default value for a variable if it is unset or empty using parameter expansion in Bash.
echo "${foo:-"DefaultValueIfFooIsMissingOrEmpty"}" # => DefaultValueIfFooIsMissingOrEmpty # This works for null (foo=) and empty string (foo=""); zero (foo=0) returns 0. # Note that it only returns default value and doesn't change variable value.
bash internaldata manipulationsstring manipulation and expansionsparameter expansiondefault value substitution
bash
This demonstrates two ways to print the current directory in Bash: using command substitution and a built-in variable.
echo "I'm in $(pwd)" # execs `pwd` and interpolates output echo "I'm in $PWD" # interpolates the variable
bash internaldata manipulationsstring manipulation and expansionscommand substitution