fbpx

What is $() in Linux?

While I was going through a tutorial, I saw $() being used in a Linux Shell command and wasn’t sure what it did.

$() is a command substitution

It turns out, $() is called a command substitution. The command in between $() or backticks (“) is run and the output replaces $(). It can also be described as executing a command inside of another command.

I find it helpful to see this in action, so here is an example:

Example of command substitution using $() in Linux:

As you can see below, if you run the following command, hostname -f, on any AWS EC2 Linux instance, you’ll be given the hostname of your current EC2 instance.

ubuntu@172-33-44-55:~$ hostname -f
ip-172-33-44-55.us-west-2.compute.internal

So, if you add hostname -f inside of $(), the output of the hostname -f command will replace $(hostname -f).

echo "Hello world from AWS Hostname $(hostname -f)"

This will result in the following:

Hello world from AWS Hostname ip-172-33-44-55.us-west-2.compute.internal

Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).

Leave a Reply

Your email address will not be published. Required fields are marked *