Command substitution in BASH

by
Ryan
on
November 7, 2008

BASH has the ability to execute a command string and replace that string with the output of the command. Or to say it another way, the output of a command will replace the command itself.

There are 2 ways to accomplish this.

The first is to surround the command with backticks or blockquotes.
`command`

You can also substitute with dollar symbol and parenthesis.
$(command)

The following will do a direct substitution of the current username on the command line.

$ echo /home/$(whoami)
/home/ryan

Another great use is the ability to assign a command to an environment variable.

$ MYDATE=`date`

or using the other format

$ MYDATE=$(date)
No Comments
bash
, , , , , ,

No related posts.

Comments (0)

No comments yet

Trackbacks (0)

No trackbacks yet

Leave a Comment

(displayed with your post)
(will not be published)
(optional)
Copyright 2008-2010 WiredRevolution.com. All rights reserved.