view the rest of the comments
Linux
Welcome to c/linux!
Welcome to our thriving Linux community! Whether you're a seasoned Linux enthusiast or just starting your journey, we're excited to have you here. Explore, learn, and collaborate with like-minded individuals who share a passion for open-source software and the endless possibilities it offers. Together, let's dive into the world of Linux and embrace the power of freedom, customization, and innovation. Enjoy your stay and feel free to join the vibrant discussions that await you!
Rules:
-
Stay on topic: Posts and discussions should be related to Linux, open source software, and related technologies.
-
Be respectful: Treat fellow community members with respect and courtesy.
-
Quality over quantity: Share informative and thought-provoking content.
-
No spam or self-promotion: Avoid excessive self-promotion or spamming.
-
No NSFW adult content
-
Follow general lemmy guidelines.
Short version:
:-only substitutes a default for that one expansion, while:=also assigns it to the variable. So: "${DB_HOST:=localhost}"andDB_HOST="${DB_HOST:-localhost}"leave you in the same place, but the first only names the variable once.The leading
:is just the no-op builtin. Its arguments still get expanded (which does the assignment), and it throws away the result so the shell doesn't try to run "localhost" as a command.A couple of details that might matter:
: "${X:=$(echo run >&2; echo v)}"twice in a row in bash, and the command substitution ran once.${1:=x}fails in both bash and dash.So your instinct about
echo "${X:=a}"is fair: it works, but it hides an assignment inside an expression. If you don't mind repeating the name,X="${X:-default}"is equivalent and arguably clearer.(Spec: POSIX Shell Command Language, section 2.6.2. I'm posting this myself with AI help for the research and for testing the examples.)