Output of echo $$ differs in Script than console


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output of echo $$ differs in Script than console
# 1  
Old 09-05-2011
Output of echo $$ differs in Script than console

Hi All,

I have a very basic query for 'echo $$' command.

When I am executing echo $$ on console then it is giving different output than the execution of echo $$ in a script.

Console Output:
-------------------------
bash-3.2$ echo $$
15610
bash-3.2$


In Script:
--------------------
bash-3.2$ cat pid.sh
echo $$
bash-3.2$


bash-3.2$ ./pid.sh
13818
bash-3.2$ ./pid.sh
13820
bash-3.2$ ./pid.sh
13823
bash-3.2$


Also why is the output changing in script everytime whereas in case of running command direct from console it is not changing everytime.


Many thanks in advance.

Thank you-Manish Kumar
# 2  
Old 09-05-2011
Well, from the name 'pid.sh' I hope you know what is $$ for. But executing a shell script your shell invokes another, child shell. And this shell is just another process. And if your start another terminal you get a different $$. Or if you just execute sh (or bash, or ksh) you get a different $$ too because all these shell would be different processes.
If you have troubles with understanding "pid" you can read more here - Process identifier - Wikipedia, the free encyclopedia.
# 3  
Old 09-05-2011
From that wikipedia link:
Quote:
In Unix-like operating systems the current process ID is provided by a getpid() system call, or as a variable $$ in shell.
That $$ statement is incorrect, though a common misconception. In a subshell the value of $$ is not the pid of the subshell, but that of an ancestor. $$ only actually reflect's the process pid when in an invoked shell's execution environment.

Take the following command:

Code:
echo $$ $(echo $$; sh -c 'echo $$')

The first $$ will match the invoked, top-level shell's pid. The second $$ is executed in a subshell whose pid is not $$, since $$ is still that of the parent, top-level (invoked) shell. The third $$ differs from the first two since it is printed from an invoked shell which sets $$ to its own pid.

If the top-level invoked shell has a pid of 10000, and the subshell created for the command subsitution is 10001, and the shell invoked from within the subshell (sh -c ...) is 10002, the output from this command would be "10000 10000 10002".

pid 10000 --> $$ 10000 (invoked shell)
pid 10001 --> $$ 10000 (subshell, pid-$$ mismatch)
pid 10002 --> $$ 10002 (invoked shell)

Personally, I don't conceive of $$ as being as much about current pid as about the current, invoked shell whose execution environment is currently in charge and propagating to subshells.

In actuality, $$ could be a pid, a parent pid (subshell), a grandparent's pid (subshell within a subshell) ...

Regards,
Alister
# 4  
Old 09-05-2011
Yes, in an "inner" subshell ( in "(...)" or "$(...)" or "`...`") you get the same $$. But I'm afraid this only confuses the topic starter. You shouldn't learn to the letter "Z" not knowing "A", is not it? Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to echo output of a UNIX command (like ls -l ) using shell script.?

How do i echo the output of a unix command using shell script??? Like: echo /etc/ ls -l (2 Replies)
Discussion started by: sunny2802
2 Replies

2. Shell Programming and Scripting

Output differs when run manually and when cron job executes it

I get a different output when i manually run the .sh script and when it is run by a cron job. Please help me .. TMP1="/lhome/bbuser/script/wslog/sar.t1" TMP2="/lhome/bbuser/script/wslog/sar.t2" TMP3="/lhome/bbuser/script/wslog/sar.t3" OUTPUT="/lhome/bbuser/script/wslog/sar.out"... (8 Replies)
Discussion started by: nithinankam
8 Replies

3. Shell Programming and Scripting

Redirect an output from a script to a file and display it at a console simultaneously

Hi, I'd like to redirect the STDOUT output from my script to a file and simultaneously display it at a console. I've tried this command: myscript.sh | tail -f However, it doesn't end after the script finishes running I've also tried this: myscript.sh | tee ~/results.txt But it writes... (3 Replies)
Discussion started by: wenclu
3 Replies

4. Solaris

No Console Output

Hey everyone I have a sparc enterprise T2000 I'm trying to install solaris 10 on. The only way I can connect to it is the SER MGT console, but I'm not getting anything to display (in Hyper terminal, or PUTTY) when I boot it. Upon googleing, all I get back is "No output may have been generated.... (4 Replies)
Discussion started by: goodvikings
4 Replies

5. UNIX for Advanced & Expert Users

PPID differs in script and prompt

I tried several times to get answer to the below problem. Someone can please help me? $ cat p1.sh #!/bin/sh `./c1.sh &` while # indefinite loop do x=5; done $ cat c1.sh # sleep for 10 sec and exit #!/bin/sh sleep 10; Execute P1 as ./p1 & $ ps -eaf | grep c1... (1 Reply)
Discussion started by: alexalex1
1 Replies

6. Shell Programming and Scripting

ksh script that echo " please insert your name " and store the output to a login.log file.

Hello All Nice to meet you all here in this forum, it's my 1rst time here i'm asking about a little issue that i face i added a ksh script that echo " please insert your name " and store the output to a login.log file. the script is working fine with normal telnet but Xstart is not working... (8 Replies)
Discussion started by: islam.said
8 Replies

7. Shell Programming and Scripting

Tailing new log file & echo the string on console

Guys, I do have a script that runs to take the server out from network, after running the script it is writing the new log file{outFile} in to directory . Now what i need is my script should tail the last modified file{outFile} & search the string {Server Status} ans should echo the same at the... (0 Replies)
Discussion started by: raghunsi
0 Replies

8. Shell Programming and Scripting

script execution differs in cron

I have the following script (MyScript): #!/bin/sh ps U erv | grep -v grep | grep -F "/usr/bin/collect -o 101" echo "Result: $?" When executed from the command line, I get ... (the line containing /usr/bin/collect -o 101) Result: 0 (which is correct since collect is running) When I... (4 Replies)
Discussion started by: lost_neko
4 Replies

9. UNIX for Dummies Questions & Answers

Writing to a file without echo it to the console !!!!

hi all, I am trying to write some message to a file using the following command. echo "${MESSAGE}" >&1 | tee -a ${File_name} can the same be done without using echo . I don't want the result to be displayed to the console. Can anyone guide me. Thanks in advance (6 Replies)
Discussion started by: amit_kv1983
6 Replies

10. Solaris

gcc output size differs on same version of solaris

Hello everybody, I am having two machines. I am using g++v2.95 When I do the build,the size of output file on both machines differ. I am not able to find what can be the problem. Both the machines are solaris 5.6 The command is : g++ -c -g -I./ -I../../../include -I/usr/local/include/g++-3... (1 Reply)
Discussion started by: manishs13
1 Replies
Login or Register to Ask a Question