72

If I'm assigning a variable with

temp=$!

what would it be its value?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Chen A.
  • 1,303
  • 2
  • 11
  • 14

2 Answers2

95

$! 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
Chris Down
  • 122,090
  • 24
  • 265
  • 262
32

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

slm
  • 363,520
  • 117
  • 767
  • 871