Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

The output of

# ls

and

# echo $(ls)

are the same. What exactly does the $ sign and the parenthesis mean?

Also, what is going on a technical level that causes the output of these two commands to be the same?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

This is command substitution for bash.

Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed as follows:

 $(command) or

 `command`

http://www.gnu.org/software/bash/manual/bashref.html#Command-Substitution

More:

http://tldp.org/LDP/abs/html/commandsub.html

http://wiki.bash-hackers.org/syntax/expansion/cmdsubst

And a good example similar to your question:

http://bashshell.net/shell-scripts/using-command-substitution-in-a-bash-shell-script/

share|improve this answer

() and $() are used to run commands in a subshell

See http://tldp.org/LDP/abs/html/subshells.html for details

share|improve this answer

Using $ in front of a variable name invokes the value assigned to the variable. Echo without the $ will just print the name of the variable to the screen (or standard out).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.