In Bash scripting, what’s the meaning of ” $! “?

If I’m assigning a variable with

temp=$!

what would it be its value?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

$! contains the process ID of the most recently executed background pipeline. From man bash:

Special Parameters

The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.

! – Expands to the process ID of the most recently executed background (asynchronous) command.

For example:

$ sleep 60 &
[1] 6238
$ echo "$!"
6238

Method 2

From Bash’s man page:

   !   Expands to the process ID of the most recently executed 
       background (asynchronous) command.

So $! would contain the process ID (PID) of the last job that was backgrounded.

Example

$ sleep 100 &
[1] 18813

$ echo "$!"
18813

References


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x