DEV Community

Cover image for Handling Arguments in Bash Scripts

Handling Arguments in Bash Scripts

Ryan Palo on February 25, 2019

Using Bash scripts to automate a set of commands is the first step in a journey of building tools and making your life easier. Simple, top-to-bott...
Collapse
 
exadra37 profile image
Paulo Renato •

Very nice article but I would like to suggest you another approach to deal with missing arguments...

Don't allow variables not set:

#!/bin/bash

set -eu

# will crash with:
# /home/exadra37/bin/test.sh: line 6: $1: unbound variable
echo $1

Give it a default value:

#!/bin/bash

set -eu

ARG="${1:-default value for argument 1}"

# will output:
# ARG1: default value for argument 1
echo "ARG1: ${ARG}"

or make it a required argument:

#!/bin/bash

set -eu

# /home/exadra37/bin/test.sh: line 5: 1:  Missing value for argument 1
ARG="${1? Missing value for argument 1}"

Collapse
 
rpalo profile image
Ryan Palo •

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!

Collapse
 
exadra37 profile image
Paulo Renato • • Edited

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="".

Thread Thread
 
xanderxaj profile image
Alex Palmer • • Edited

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:

rm -rf "$STEAMROOT/"*

Moving the Steam install location would cause STEAMROOT to be blank...

rm -rf "/"*

Let's hope you didn't run it as root.

Collapse
 
kgoutham93 profile image
Goutham Kolluru •

What other options of set are typically used in bash scripts?

Collapse
 
rpalo profile image
Ryan Palo •

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 :)

Collapse
 
bewuethr profile image
Benjamin Wuethrich •

If you quote brace expansion, as in

mkdir -p "${project_name}/{CAD,drawings,mold,resources}"

it'll create a directory that's literally called {CAD,drawings,mold,resources}; brace expansion must not be quoted.

Collapse
 
rpalo profile image
Ryan Palo •

Ooh good catch. You’re totally right. Fixing now. Thanks!

Collapse
 
gorliver profile image
gorliver •

Thank you so much for your article and I really enjoy it!
Looking forward to the "fancier argument parsing".

Collapse
 
rpalo profile image
Ryan Palo •

Thanks! I’m glad you liked it! Yep! Coming soon!

Collapse
 
oscherler profile image
Olivier “Ölbaum” Scherler •

I’m looking forward to the next post, because I got stuck with argument processing for more complex scripts a couple of times.

Collapse
 
nebojsac profile image
Nick Cinger •

Very useful, thanks for breaking it down :)

Collapse
 
bogdaaamn profile image
Bogdan Covrig •

Bookmarked right away! I always mess them up, thanks for putting them together.

Collapse
 
rpalo profile image
Ryan Palo •

Glad it could help you out!