Last active
July 25, 2017 12:34
-
-
Save bogovicj/84d513bd1841ad9bc9f7ae9b8360b0f8 to your computer and use it in GitHub Desktop.
bashParameterExpansion.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# See | |
# https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion | |
# ${parameter:-word} substitutes word if parameter is unset | |
echo '${parameter:-word}' | |
dog="dog" | |
echo "${dog:-"cow"}" # Prints "dog" | |
echo "${cow:-"cow"}" # Prints "cow" | |
echo "${cow}" # Prints nothing (cow is unset) | |
# ${parameter:=word} sets parameter equal to word if parameter is unset, then substitutes parameter | |
echo '' | |
echo '${parameter:=word}' | |
echo "${dog:="cow"}" # Prints "dog" | |
echo "${cow:="cow"}" # Prints "cow" | |
echo "${cow}" # Prints "cow" | |
# ${parameter:?word} Prints word on stderror if parameter is unset | |
echo '' | |
#echo '${parameter:?word}' | |
#echo "${cat:?"A cat related error"}" # Prints "./bashParameterSubstitution.sh: line 17: cat: A cat related error" and returns 1 | |
# ${parameter:+word} Prints word on stderror if parameter is unset | |
echo '' | |
echo '${parameter:+word}' | |
echo "${cat:+"a cat related success"}" # Prints nothing since cat is unset | |
cat="garfield" | |
echo "${cat:+"a cat related success"}" # Prints "a cat related success" | |
# ${parameter:offset:length} | |
string="0123456789abcdef" # 16 values | |
echo "${string:1}" # Cut off the first value | |
echo "${string:0:-1}" # Cut off the last value | |
echo ${string:7:2} # Offset by 7, length 2 | |
echo ${string:7:-2} # start offset by 7, end 2 from the end | |
# Prefix | |
echo ' ' | |
metacow="metacow" | |
metacat="metacat" | |
metadoe="metadoe" | |
postdog="postdog" | |
postowl="postowl" | |
echo "${!meta*}" # Prints "metacow metacat metadow" | |
echo "${!meta@}" # Prints "metacow metacat metadow" as separate words | |
echo "${!post*}" # Prints "postdog postowl" | |
# Length | |
echo ' ' | |
echo "${#metacow}" # Prints "7" | |
echo "${#dog}" # Prints "3" | |
# Delete from start | |
echo "${metacow#meta}" # Prints cow | |
# Shortest matching pattern | |
echo "${postowl#*o}" # Prints stowl | |
# Longest matching pattern | |
echo "${postowl##*o}" # Prints wl | |
# A practical example - keep the file part of a path | |
mypath="/some/path/to/a/file" | |
echo "${mypath##*/}" # Prints "file" | |
# Delete from the end | |
echo ' ' | |
echo "${metacow%c[a-z][a-z]}" # Prints meta | |
# Shortest matching pattern | |
echo "${metacat%a*}" # Prints metac | |
# Longest matching pattern | |
echo "${metacat%%a*}" # Prints met | |
# A practical example - keep the directory part of a path | |
echo "${mypath%/*}" # Prints /some/path/to/a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment