Bash:
Interpolating a string
How to:
Bash strings play nice with variables. Drop a variable into a string with some curly braces, and you’re golden.
name="World"
greeting="Hello, ${name}!"
echo $greeting
Output:
Hello, World!
Bash says, “Keep it flexible.” Change name
, and your greeting follows suit.
name="Bash Pros"
greeting="Hello, ${name}!"
echo $greeting
Output:
Hello, Bash Pros!
Deep Dive
Back in the day, programmers glued strings together with concatenation. It got messy. String interpolation swooped in like a superhero for cleaner, more readable code.
Bash, unlike some other languages, doesn’t fuss—just a dollar sign and some braces. Other languages dress it up with special syntax or functions. In Bash, it’s all about those braces and the occasional escape character if you’re feeling fancy.
Some alternatives? Sure, you can concatenate or use echo
without braces if you’re not doing anything complex. But why settle?
As for implementation, when Bash sees ${}
, it grabs the variable value and swaps it in, no questions asked. This makes sure what you see (in your code) is what you get (in your output).
See Also
For more on string magic:
- Bash String Manipulation: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
- Advanced Bash-Scripting Guide: https://tldp.org/LDP/abs/html/
- Stack Overflow (practical examples for real-world problems): https://stackoverflow.com/questions/tagged/bash