push() {
local a="$1" b="$2" #don't overwrite global variables
[ "$#" -eq 0 ] && {
1>&2 echo "Enter git shortname for first argument" #stderr
return 64 #EX_USAGE=64
}
: "${b:=`timestamp`}" #default value
git add -A .
git commit -m "$b" #always quote variables, unless you have a good reason not to
git push "$a"
1>&2 echo "push() completed."
}
The above should run in dash
as well as bash
, if case you want to make use of the faster startup time.
Why quote?
If you don't quote, variables will get split on characters present in $IFS
, which by default is the space, the newline, and the tab character. Also, asterisks within the contents of unquoted variables are glob-expanded. Usually, you want neither the splitting nor the glob expansion, and you can unsubscribe from this behavior by double quoting your variable.
I think it's a good practice to quote by default, and comment the cases when you don't quote because you really want the splitting or the globbing there.
(Bash
doesn't split or glob-expand in assignments so local a=$1
is safe in bash
, however other shells (most prominently dash
) do. a="$1"
is ultra-safe, consistent with how variables behave elsewhere, and quite portable among shells.)