LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-12-2009, 06:00 PM   #1
bencharluo
LQ Newbie
 
Registered: May 2009
Location: Hangzhou China
Distribution: Ubuntu
Posts: 17

Rep: Reputation: 1
Question $$


Hi,everybody.
I have a question now.
I write the code followed in test.sh:

#!/bin/bash

var tem_file=/tmp/cdb.$$
echo ${tem_file}
exit 0

When I execute sh test.sh.
It shows : /tmp/cdb.4639.
I execute it once more.
It shows : /tmp/cdb.4640.
and more and more.
They show: /tmp/cdb.4641. /tmp/cdb.4642....

Can anyone tell me why?
And I can't find /tmp/cdb.4639 and others file...
Thank you very much!
 
Old 05-12-2009, 06:10 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
1. drop the 'var', this is bash not Pascal(?)
2. $$ is the pid of the current process. These are handed out by the kernel, and you have no control over them. Other processes are continuously being created that you don't notice.
They are a montonically increasing num that wraps after 4 or 5(?) digits, but never 2 processes with same pid(!)
HTH
 
Old 05-12-2009, 06:15 PM   #3
bencharluo
LQ Newbie
 
Registered: May 2009
Location: Hangzhou China
Distribution: Ubuntu
Posts: 17

Original Poster
Rep: Reputation: 1
you mean when I exec sh test.sh.
The kenel hand out a process ID to the program?
 
Old 05-12-2009, 06:28 PM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
There's no mystery that the number increases. $$ expands to the PID of the current shell, and each time you run the script it's run into a new shell. So, it's simple maths.

I have no idea about what you are trying to do. Maybe if you explain that we can help you. If what you wanted is to access a file that's called literally 'scd.$$', then use single quotes around it so the $$ is not expanded.

EDITED:

Quote:
Originally Posted by bencharluo View Post
you mean when I exec sh test.sh.
The kenel hand out a process ID to the program?
When you run a shell script, it doesn't matter if you use "sh script.sh" or "./script.sh" to run it, a new shell is spawned. The script is not run in your current shell, but into a new session with it's own local environment. The new shell is a new process, with a new unique PID. The pid of this new shell is what $$ will return. For more info read the bash man page.

Last edited by i92guboj; 05-12-2009 at 06:44 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration