Command Substitution
In this lesson, you'll learn about command substitution and other ways of substituting commands.
How Important is this Lesson?
The contents of this lesson are vital if you intend to write or work on shell scripts.
Command Substitution Example
When writing bash scripts you often want to take the standard output of one command and ‘drop’ it into the script as though you had written that into it.
An example may help illustrate this idea. Type these commands:
Press + to interact
hostname # 'hostname' outputs the name of the hostecho 'My hostname is: $(hostname)' # Single quotes do not call the hostname commandecho "My hostname is: $(hostname)" # Double quotes do, similar to variable dereferencing
If those lines are placed in a script, it will output the hostname of the host the script is running on. This can make your script much more dynamic. You can set variables based on the output of commands, add debug, and so on, just as with any other programming language.
You may have noticed that if wrapped ...