Mastering $() and ${} Expansions in Bash Scripting
As a Linux system administrator, getting the most out of your shell and scripting workflow is critical for automating tasks and harnessing the power of your environment. Two of the most useful features for unlocking this potential are the $()
and ${}
expansions available in Bash and other shells.
Understanding Shell Expansions
An expansion in Bash happens when a token is replaced with its value before the shell executes each command. The token can be special characters, a variable, or command substitution syntax telling the shell to evaluate part of the script and insert the output.
Expansions allow your scripts to dynamically adapt to different situations. You can set variables based on your environment, run commands on the fly, check for errors, set default values, and more.
Command Substitution with $()
$()
is the syntax for command substitution in Bash. It runs the command inside the $()
and replaces it with the standard output.
For example:
today=$(date)
This will set the today
variable to the result of running date
.
The alternative syntax is backticks `date`
but $() is preferred for improved readability.
Common use cases for command substitution include:
- Setting dynamic variables based on output of other programs
- Inline math operations from tools like
bc
- Reading files into variables to parse contents
- Embedding output of one command as argument to another
Parameter Expansion with ${}
${}
provides a syntax for variable expansion and modifying parameters in Bash. Inside the curly braces you can reference variables or manipulate them in useful ways.
For example:
name="John"
echo "${name} Doe"
This prints out "John Doe" by expanding the $name
variable.
Parameter expansion is useful for:
- Getting values of variables
- Setting default values if undefined with
-
- Preventing splits with a prefix
:
- Signaling errors with a prefix
?
- And more
The main difference from command substitution is that ${}
directly expands variables rather than running commands.
Putting Them Together in Scripts
In practice, shell scripts will utilize both $()
and ${}
to get the environment information needed for the task at hand.
For example, this script prints a system status report using command substitution to find dynamic data:
#!/bin/bash
echo "*** Status Report ***"
today=$(date)
echo "Today is ${today}"
users=$(who | wc -l)
echo "${users} users logged in"
uptime=$(uptime)
echo "Uptime: ${uptime}"
The key is knowing when to reach for which expansion syntax based on what you need to accomplish in your script.
In Closing
Getting fluent with $()
and ${}
allows you to write more robust and flexible Bash scripts. You can query outside sources for dynamic data instead of hardcoding values. And handle variables in a smoother way.
With the techniques discussed here you will level up your shell scripting and save time in your Linux admin role. The Bash shell gives you access to all the underlying UNIX tools, and these expansions help combine them into automated solutions.