24

I saw the following syntax in a bash script:

 >$(command) &

I know that the ampersand & at the end makes a command run in the background, but I have never seen it in combination with the dollar sign plus the brackets around the command.

What does it do?

3
  • I know that kind of usage of $( ), but there is no rest. It is just $(command) &, that's what confuses me.
    – AlphaOmega
    Commented Oct 6, 2016 at 14:32
  • what about eval $(cmd)? Commented Jun 7, 2022 at 21:41
  • related stackoverflow.com/questions/30155960/… its a useful concrete real example imho for eval $(opam env) as the example. Commented Jun 8, 2022 at 15:34

2 Answers 2

34

Short Answer

Given the syntax $(command) &, the result is the same as if you took the output of running just command and then tried to run that as a command. And all of this would be done in the background, meaning that there would be no visible output or hanging in the terminal.

For example, if we had a text file named mycommand.txt with the contents:

echo "Hello World"

Then the command

$(cat mycommand.txt) &

would be equivalent to running

echo "Hello World" &

But since we added the ampersand (&), we wouldn't see any output from this command, so it would be pretty useless in this example.


Long Answer (Explanation)

Dollar sign $ (Variable)

The dollar sign before the thing in parenthesis usually refers to a variable. This means that this command is either passing an argument to that variable from a bash script or is getting the value of that variable for something. The difference in bash scripting for calling on and declaring variables goes as such:

Declare a variable without the dollar sign and call it with a dollar sign. For example, a script that contains this

#!/bin/bash
STR="Hello World!"
echo $STR

Would output this

Hello World!

Parenthesis () - Command substitution

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`

Bash performs the expansion by executing the command in a subshell environment and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

Ampersand & - Background process

And then the ampersand, or & symbol, is as you said, used for running it in the background.

3
  • what about eval $(cmd)? Commented Jun 7, 2022 at 21:41
  • 1
    related stackoverflow.com/questions/30155960/… its a useful concrete real example imho for eval $(opam env) as the example. Commented Jun 8, 2022 at 15:35
  • Wasn't the question about ` >$(command) &? In that case you'd empty or create a file with a name that the output of command`specifies. The only reason to do this in the background could be opening some devices that might block. In general the original question is rather abstract to know the details.
    – U. Windl
    Commented Feb 15, 2023 at 23:26
2

It takes the output of command as a command and runs that in the background, its functionally is comparable with eval.

2
  • It is a symbol present in the terminal window which depending on what you are trying to do may not be needed for entry by you. When you open a terminal you will see it there immediately. Commented Oct 6, 2016 at 14:42
  • I don't understand. Although short, this answers the actual question while the accepted answer doesn't.
    – mveroone
    Commented Jun 13, 2017 at 12:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .