Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 05-25-2010, 06:13 AM   #1
Registered: Oct 2007
Posts: 30

Rep: Reputation: 15
How is the "Current Working Directory Maintined"


I was wondering how the current working directory is maintained in linux. I assumed it would simply be an environment variable, but all attempts i've made to change directory by resetting variables shown by env have failed.

Is this something that is maintained internally in the command prompt (bash in my case)?

The background to this is I'm setting up a script to remotely run commands over ssh. Since separate commands will unfortunately require separate SSH connections, I need to maintain (save and reload) items such as environment variables and current working directory from one call of the script to the next.

Thanks for reading
Old 05-25-2010, 06:45 AM   #2
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
The current working directory is an attribute of each process and can be changed by the program in the process using the chdir(2) system call. The shell command cd makes the shell call chdir(2) and then set the $PWD special variable accordingly. The shell's command prompt string is often set to display the current working directory by the "special character" \w as documented here.
1 members found this post helpful.
Old 05-25-2010, 06:56 AM   #3
LQ 5k Club
Registered: May 2003
Location: London, UK
Distribution: Debian "Testing"
Posts: 6,096

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
pwd will tell you your current directory, as will echo $PWD on my system
To change to another directory, use cd /path/to/dir
$PWD will be updated to the new directory
You can save these paths in an environment variable, and re-use them later.

Try this:

cd /
cd $whereami
You might also look at pushd and popd

pushd $(pwd)
cd /
Old 05-25-2010, 07:10 AM   #4
Registered: Oct 2007
Posts: 30

Original Poster
Rep: Reputation: 15
Thanks. Thats the information I was looking for...

It does however raise one concern. What other "attributes" do i need to be aware of?
I'm trying to run a process a remote machine to mimic running on the local machine.

I've covered:
  • using a mounted smb share to unify relevant parts of the file system
  • stdin/stdout/stderr,
  • environment variables (minus 1 or two such as PATH),
  • process return value
  • and now working directory

Off the top of your head, is there any other attribute that I may need to copy across in order to get one system to mimic another?
Old 05-25-2010, 08:55 AM   #5
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Depends on what you're trying to do. A couple of things that may be germane would be user & group ownership of the process, and the type of TTY to which the process is attached (if any). should give you some decent background.
--- rod.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Limit "ls -la a*" to Current Directory Canis Polaris Linux - Newbie 3 03-13-2009 01:55 PM
Format command shows "Cannot get current directory - I/O error" devn Solaris / OpenSolaris 1 06-18-2008 01:26 AM
net working eth0 eth1 wlan0 "no connection" "no LAN" "no wi-fi" Cayitano Linux - Newbie 5 12-09-2007 07:11 PM
Problem with X consoles - "shell-init: error retrieving current directory" lowpro2k3 Slackware 4 03-26-2007 06:36 PM
"xv" video driver not working in slackware-current athanatos Slackware 4 08-31-2006 02:27 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:45 PM.

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