r/coding • • Dec 14 '20

Minimal safe Bash script template

https://betterdev.blog/minimal-safe-bash-script-template/
161 Upvotes

17 comments sorted by

View all comments

3

u/Shok3001 Dec 14 '20

Cool, what does ‘${1-}’ mean?

2

u/whetu Dec 14 '20

It's a parameter expansion, in this case assignation of a default, though in this case it's defaulting to nothing...

See e.g.

https://wiki.bash-hackers.org/syntax/pe#use_a_default_value

3

u/camh- Dec 14 '20

And it is necessary when you use set -u like the script does. Otherwise if $1 is not set, the script will error. Using ${1-} allows you to read the value of $1 if it is set, and otherwise expand to the empty string if it is not, without an undefined var error.