Linux – Questions about Linux commands

Questions about Linux commands… here is a solution to the problem.

Questions about Linux commands

What do

the two “&” signs in the following command do:

(make foo&)&

Solution

Run the command in the subshell ( and ) This means that a separate shell will be spawned and the command will be run. This may be because they want to use shell-specific actions (background – other examples are redirects, etc.). The first & in the command causes the command to run in the subshell (i.e. make foo). The second symbol serves as the background for the subshell itself so that you immediately return to the command prompt.

The effect can be seen here

The foreground of the current shell

(bb-python2.6)noufal@NibrahimT61% ls # shell waits for process to complete
a  b  c  d  e

The background of the current shell

(bb-python2.6)noufal@NibrahimT61% ls& #Shell returns immediately.
[1] 3801
a  b  c  d  e
[1]  + done       /bin/ls -h -p --color=auto -X

Use a subshell (foreground).

(bb-python2.6)noufal@NibrahimT61% (ls&) # Current shell waits for subshell to finish.
a  b  c  d  e                           

In this case, the current shell waits for the child shell to complete, even though the job itself in the child shell is running in the background.

Use a subshell (background).

(bb-python2.6)-130- noufal@NibrahimT61% (ls &)&
[1] 3829
a  b  c  d  e
[1]  + exit 130   (; /bin/ls -h -p --color=auto -X &; )

The foreground shell returns immediately (a subshell that does not wait for ls to finish by itself). Observe the difference in command execution.


A marginal note about the need to run some commands in the subshell. Assuming you want to run a “shell command” (i.e. using something shell-specific, like redirects, job IDs, etc.), you must run the command in a child shell (using ( , )) or use the -c option for shells such as bash. You can’t directly exec things like that because the shell is necessary to handle job IDs or something. Ampersanding will make the subshell return immediately. The second symbol in your code looks (as other answers suggest) redundant. A case of “make sure it’s contextualized”.

Related Problems and Solutions