3 ms·
Neat tricks, thanks for sharing! In case anyone's interested, I recently came across a neat trick in Bash to make sure the user specifies a command line argume
by raboukhalil 11y ago
Neat tricks, thanks for sharing!
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.
- bburky 11y agoNice trick. It looks like this is actually a POSIX shell feature too, so feel free to use this in portable shell scripts even. Here's the rest of the parameter expansion options. I usually only remember half of them. http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3...