I want to test if an augment (e.g. -h) was passed into my bash script or not.
In a Ruby script that would be:
#!/usr/bin/env ruby
puts "Has -h" if ARGV.include? "-h"
How to best do that in Bash?
I want to test if an augment (e.g. -h) was passed into my bash script or not.
In a Ruby script that would be:
#!/usr/bin/env ruby
puts "Has -h" if ARGV.include? "-h"
How to best do that in Bash?
I found the answer in a dupe question here: https://serverfault.com/questions/7503/how-to-determine-if-a-bash-variable-is-empty
See my function mt() below for an example usage:
As Jonathan Leffler pointed out OPTIND=0 will reset the getopts list. That's in case the test needs to be done more than once.
The simplest solution would be:
It is modestly complex. The quickest way is also unreliable:
Unfortunately that will also spot "
command this-here
" as having "-h
".Normally you'd use
getopts
to parse for arguments that you expect:Etc.