by raboukhalil · 10y ago · 0 comments
In case anyone's interested, I recently came across a neat trick in Bash to make sure the user specifies a command line argument.
Instead of:
if [ -z "$1" ]; then echo "Error!!"; exit; fi
This basically does the same thing and is more elegant (especially if you need to do this for many arguments): var=${1?Error!!}
If the user specifies an argument, it will be saved in $var; if not, Bash will output an error message and will stop execution of the program.Here's the rest of the parameter expansion options. I usually only remember half of them.
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3...