LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How is the "Current Working Directory Maintined" (https://www.linuxquestions.org/questions/linux-newbie-8/how-is-the-current-working-directory-maintined-809972/)

Couling 05-25-2010 06:13 AM

How is the "Current Working Directory Maintined"
 
Hi

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

catkin 05-25-2010 06:45 AM

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.

tredegar 05-25-2010 06:56 AM

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:

Code:

pwd
whereami=$(pwd)
cd /
pwd
cd $whereami
pwd

You might also look at pushd and popd

Code:

pwd
pushd $(pwd)
cd /
pwd
popd
pwd


Couling 05-25-2010 07:10 AM

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?

theNbomr 05-25-2010 08:55 AM

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). http://tldp.org/HOWTO/Secure-Program...processes.html should give you some decent background.
--- rod.


All times are GMT -5. The time now is 07:16 AM.