Understanding Command Substitution
In the Counting Logged-in Users and Using Arrays Hands-on Labs, I’ve shown you some examples of command substitution in action, but I haven’t yet fully explained it. It’s about time that I do.
Command substitution is an extremely handy tool that you will use extensively. I mean, really. You can do some very cool stuff with it. It involves taking the output from a shell command and either using it in another command, or assigning it as the value of some variable. You’ll place the command from which you’ll be taking output within a $( )
construct. Here’s a very simple example:
[donnie@fedora ~]$ echo "This machine is running kernel version $(uname -r)."
This machine is running kernel version 6.5.5-200.fc38.x86_64.
[donnie@fedora ~]$
You see how the output of the uname -r
command, which shows the version of the current running Linux kernel, is substituted for the command substitution construct...