Bootstrap FreeKB - Linux Fundamentals - Command substitution
Linux Fundamentals - Command substitution

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


Please enter 41de5b in the box below so that we can be sure you are a human.