Linux Fundamentals - Command substitution

by
Jeremy Canfield |
Updated: March 20 2020
| Linux Fundamentals articles
Let's write some text. In this example, we do not want to echo the word "date." Instead, we want to use the date command.
[user1@server1 ~]# echo "Today is date"
Today is date
Command substitution can be used to use the date command. There are two ways to format substitution:
- $(command)
- `command`
[user1@server1 ~]# echo "Today is $(date)"
Today is Jan 01 00:00:01 CDT 2016
[user1@server1 ~]# echo "Today is `date`"
Today is Jan 01 00:00:01 CDT 2016
Another example:
[user1@server1 ~]# echo "My computer name is $(hostname)"
My computer name is server1
[user1@server1 ~]# echo "My computer name is `hostname`"
My computer name is server1
Options can be used in the command:
[user1@server1 ~]# echo "Kernel version: $(uname -r)"
Kernel version: 3.18.3-031803-generic
[user1@server1 ~]# echo "Kernel version: `uname -r`"
Kernel version: 3.18.3-031803-generic
Did you find this article helpful?
If so, consider buying me a coffee over at