Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In UNIX shell scripting: What does $! expand to?

Tags:

shell

unix

ksh

What is the meaning for $! in shell or shell scripting? I am trying to understand a script which has the something like the following.

local@usr> a=1
local@usr> echo $a
1
local@usr> echo $!a
a

It is printing the variable back. Is it all for that? What are the other $x options we have? Few I know are $$, $*, $?. If anyone can point me to a good source, it will be helpful. BTW, This is in Sun OS 5.8, KSH.

like image 302
Guru Avatar asked Jan 24 '23 01:01

Guru


2 Answers

The various $… variables are described in Bash manual. According to the manual $! expands to the PID of the last process launched in background. See:

$ echo "Foo"
Foo
$ echo $!

$ true&
[1] 67064
$ echo $!
67064
[1]+  Done                    true

In ksh it seems to do the same.

like image 76
zoul Avatar answered Jan 29 '23 07:01

zoul


From the ksh man page on my system:

  ${!vname}
      Expands  to the name of the variable referred to by vname.  This
      will be vname except when vname is a name reference.
like image 36
Greg Hewgill Avatar answered Jan 29 '23 08:01

Greg Hewgill