DEV Community

loading...
Cover image for TIL about shell parameter expansion in Bash
Octopus Deploy

TIL about shell parameter expansion in Bash

Ryan Rousseau
tech, comedy, cats, mostly cats, Continuous Delivery Architect @OctopusDeploy, opinions are mine, he/him
Originally published at blog.rousseau.dev ・1 min read

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.

${parameter:+word}
    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 blog.rousseau.dev. Cover photo by NASA on Unsplash.

Discussion (0)