Wednesday, May 18, 2016

Bash default value environment variable that can be overridden

Often in bash scripts I want to have a constant that is overridable. It's something I expect people to want to change but isn't worth creating commandline options for.

Here's how to do it:
#!/bin/bash

: ${OVERRIDABLE:="thedefault"}

echo ${OVERRIDABLE}
And it works like this:
$ bash ./temp.sh 
thedefault
$ OVERRIDABLE="overridden" bash ./temp.sh 
overridden

No comments: