Setting default values in Bash

Index, feed.

[ Date | 2020-01-31 18:45 -0500 ]
[ Mod. | 2020-02-14 11:19 -0500 ]

This article is about an idiom to set default values for variables in Bash shell scripts; that is, values that can be overridden by setting variables prior to running the script.

Idiom

: "${ID:=value}"

This will make it so that ID retains its value, if it had one, defaulting to value otherwise.

Explanation:

I wish this was a more widely-known idiom, so that programmers would not be confused by it.

Note: A previous version of this article complained about ShellCheck flagging the unquoted version of the construct, but I now believe the rationale for SC2223 is sensible.

www.kurokatta.org


www.kurokatta.org

Quick links:

Photos
Montréal
Oregon
Paris
Camp info 2007
Camp Faécum 2007
--more--
Doc
Jussieu
Japanese adjectives
Muttrc
Bcc
Montréal
Couleurs LTP
French English words
Petites arnaques
--more--
Hacks
Statmail
DSC-W17 patch
Scarab: dictionnaire de Scrabble
Sigpue
Recipes
Omelette soufflée au sirop d'érable
Camembert fondu au sirop d'érable
La Mona de Tata Zineb
Cake aux bananes, au beurre de cacahuètes et aux pépites de chocolat
*