top | item 32163911

(no title)

dzove855 | 3 years ago

Creator here of this server.

: = does nothing but the := in the variable declariation will assign set the value of BASH_LOADABLE_PATH to /usr/lib/bash if the variable is empty.

So instead of writing: BASH_LOADABLE_PATH=${BASH_LOADABLE_PATH:-/usr/lib/bash}

you can just do : : "${BASH_LOADABLE_PATH:=/usr/lib/bash}"

discuss

order

Brian_K_White|3 years ago

And the initial : is there to create a valid executable line where variables and brace expansions are actually evaluated, but without actually doing anything else. : is a synonym for true.

Sort of like a comment in that no statement is executed (well a statement is executed, it's just a almost-no-op statement) but unlike a comment in that the braces are evaluated, and any actions they perform like assignments, are evaluated and performed.

Most brace expansions just expand something in place, they essentially mostly just read and display something, possibly modifying it along the way from memory to output. But a few brace expansions actually do assignments. And that "set to this value if not already set to anything" is very useful. So a common trick is things like

:${DEBUG:=false}

: is just a synonym for "true" ie a valid command that does nothing but also does not produce an error and also is an executable statement not a comment.

Everything after the true is evaluated for real just like if true were instead echo or any other command.

The end result of that debug example is now all through the rest of the script you can put $DEBUG && echo doing step 7...

and never have to worry about the invalid syntax condition if DEBUG had not been set to something. All else being equal, it's always going to be set to false, and so all those debug commands will be valid syntax.

But you can enable verbose mode later at run-time without editing the script by running

DEBUG=true myscript

The := brace expansion means that since DEBUG was set to something, it's not modified, not set to false. it's only set to false if it was unset to begin with.

It's basically the shortest way to define a configurable with a default value at the same time.