Friday, October 7, 2022

[SOLVED] What does "$" and "!" mean in shell command?

Issue

I am learning nanomsg with its example. But the command line below made me confused.

./pipeline node0 ipc:///tmp/pipeline.ipc & node0=$! && sleep 1

I can understand that ./pipeline refers to the executable file and node0 together with ipc:///tmp/pipeline.ipc are the parameters passed to the program. The & symbol means the command is executed in the background. According to this answer, if node0=$! executed successfully then the terminal will sleep for 1 second. Here comes the problem:

What does node0=$! mean? Especially what does $ and ! mean here?


Solution

"$" and "!" does not mean anything (only literals). But "$!" does mean something.

"$!" is the PID of the last program sent to background.

Example:

$ sleep 60 &
[1] 21657
$ echo $!
21657

And node0 is a variable with the PID of the previous command sent to background (./pipeline in this case).

Note: the PID may differ.



Answered By - sudoer
Answer Checked By - Katrina (WPSolving Volunteer)