Golden Codes - armanexplorer planet

Practical code snippets for Django, Python, Bash, Git and All!

View on GitHub

substring expansion

${1:0:1}: This is a substring expansion. It extracts a substring from $1, starting from the 0th character (the first character) and with a length of 1 character.

param expansion

Docs

Good Ref

Other ref

Note: we can use : (colon, bash built-in command) to do nothing and be a placeholder to allow the expansion of the variable in the next command:

: "${POSTGRES_HOST_AUTH_METHOD:=}"

shell substitution example

${x:=y} (Assign if unset):

${x:-y} (Default if unset):

Here's a table summarizing the behavior:

Scenario ${x:=y} ${x:-y}
x is unset or empty Assigns y to x, expands to y Expands to y
x is set (even to "") Expands to current value of x (might overwrite with y for :=) Expands to current value of x

Choosing the right substitution: