Command substitution in BASH

by
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
, , , , , ,

Related posts:

  1. Customize the BASH PS1 command prompt
  2. Single versus double quotes in BASH
  3. Search for files with the find command
  4. Have a Bash script determine it’s own location
  5. Find the exit status of a previous command in Bash

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.