I am a Developer Advocate for Security in Mobile Apps and APIs at approov.io. Another passion is the Elixir programming language that was designed to be concurrent, distributed and fault tolerant.
Location
Scotland
Education
Self teached Developer
Work
Developer Advocate for Mobile and API Security at approov.io
Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
You're totally right, this is a good way to do it too. Thanks! Personally, I don't like set -u, because I like having blank variables, but I'm aware that maybe that makes me weird.
That being said, knowing about default and required parameter syntax is a really useful and important skill, and I'm definitely going to add it to my "Advanced Argument Handling" post. Thanks for bringing it up!
I am a Developer Advocate for Security in Mobile Apps and APIs at approov.io. Another passion is the Elixir programming language that was designed to be concurrent, distributed and fault tolerant.
Location
Scotland
Education
Self teached Developer
Work
Developer Advocate for Mobile and API Security at approov.io
The -u flag have saved me a lot of bugs and can save you to delete the wrong path in your disk rm -rf ~/${UNSET_VAR} will be a disaster when used in some script.
If you like to have empty vars in your script then be explicit and initiate them with VAR="".
Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Usually I see set -o errexit, set -o nounset, and set -o pipefail as safety catches. Ben is right on about set -x. That’s a great one for debugging. Googling or running ‘man set’ will give you a bunch more info :)
I've been a professional C, Perl, PHP and Python developer. I'm an ex-sysadmin from the late 20th century. These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Very nice article but I would like to suggest you another approach to deal with missing arguments...
Don't allow variables not set:
Give it a default value:
or make it a required argument:
You're totally right, this is a good way to do it too. Thanks! Personally, I don't like
set -u
, because I like having blank variables, but I'm aware that maybe that makes me weird.That being said, knowing about default and required parameter syntax is a really useful and important skill, and I'm definitely going to add it to my "Advanced Argument Handling" post. Thanks for bringing it up!
The
-u
flag have saved me a lot of bugs and can save you to delete the wrong path in your diskrm -rf ~/${UNSET_VAR}
will be a disaster when used in some script.If you like to have empty vars in your script then be explicit and initiate them with
VAR=""
.This reminds me of that time running Steam (the PC video game client) would delete everything your user could delete on Linux.
It ran this code:
Moving the Steam install location would cause
STEAMROOT
to be blank...Let's hope you didn't run it as
root
.What other options of
set
are typically used in bash scripts?Usually I see set -o errexit, set -o nounset, and set -o pipefail as safety catches. Ben is right on about set -x. That’s a great one for debugging. Googling or running ‘man set’ will give you a bunch more info :)
-x
prints out every command as it's run, which can be fun (if verbose) for debugging.If you quote brace expansion, as in
it'll create a directory that's literally called
{CAD,drawings,mold,resources}
; brace expansion must not be quoted.Ooh good catch. You’re totally right. Fixing now. Thanks!
I’m looking forward to the next post, because I got stuck with argument processing for more complex scripts a couple of times.
Very useful, thanks for breaking it down :)
Thank you so much for your article and I really enjoy it!
Looking forward to the "fancier argument parsing".
Thanks! I’m glad you liked it! Yep! Coming soon!
Bookmarked right away! I always mess them up, thanks for putting them together.
Glad it could help you out!