How to substitute a command in Linux

Home > Search > Linux commands > How-to
  by

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

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments