# This script template was copied from https://gist.github.com/ErezBinyamin/132c576b132f397ae63f7059d72ff5c9
# Save this script as some unique SCRIPTNAME.sh
# For starters rename the 'SCRIPTNAME' variable: `sed -i s/SCRIPTNAME/desiredname/g`
SCRIPTNAME() {
# Argparsing envars and positional arguments into meaningfully named local variables. Don't do optargs in bash scripts
local ENVAR=${ENVAR:-defaultValueString}
local ARG1=${1:-7}
[[ $# -lt 0 || $# -gt 3 ]] && return 1
helperFunction() {
echo "Incremented ARG1 from ${ARG1} to $((ARG1++))"
export ARG1
}
# Main
helperFunction
return 0
}
# Run if called with `bash SCRIPTNAME.sh`, Do not run if called by `source SCRIPTNAME.sh`
[[ "$0" =~ "SCRIPTNAME.sh" ]] && SCRIPTNAME $@
There are some lesser known bash variable expansions:
description | expression |
---|---|
Remove everything after the last '7' | ${var%7*} |
Remove everything after the first '7' | ${var%%7*} |
Remove everything before the first '7' | ${var#*7} |
Remove everything before the last '7' | ${var## *7} |
First char upper case | ${var^} |
All upper case | ${var^^} |
First char lower case | ${var,} |
All lower case | ${var,,} |
Show how variable was set | ${var@A} |
?? something cool ?? | ${var@E} |
Print variable as though it were the prompt variable PS1 | ${var@P} |
?? something cool ?? | ${var@Q} |
I have been struggling to find a source that documents all of these tricks. So far the best one I have found is curl cheat.sh/bash/:learn
. But even that page is missing these expansion rules.