DEV Community

Cover image for TIL about shell parameter expansion in Bash
Ryan Rousseau for Octopus Deploy

Posted on • Originally published at

TIL about shell parameter expansion in Bash

The problem

I am working on a new Octopus step template for Firebase deployments. I want to make the template shareable, so it should support all of the deploy command parameters.

I do not write Bash scripts very often. When I do, I do not usually plan to share them, so I write them to suit only my use case. I quickly ran into a problem I did not have a solution to.

Take the message parameter of the Firebase deploy command.

firebase deploy -m "A helpful comment about this deployment"

Message is optional. I only want to pass it to the command if a user of the template has provided it.

The solution

The solution is shell parameter expansion.

    If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

Perfect! What does that look like in practice?

message=$(get_octopusvariable "FirebaseDeploy.Message")

firebase deploy ${message:+ -m "$message"}

I will be writing more Bash scripts, so it is good to have learned this feature early on.

What is your favorite Bash tip or trick?

This post was originally published at Cover photo by NASA on Unsplash.

Top comments (0)