LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 01-13-2013, 10:44 PM   #1
vitop
LQ Newbie
 
Registered: Jan 2013
Posts: 3

Rep: Reputation: Disabled
Change directories without cd? (or a variant)


I am trying to change directories without using cd (or popd or any variant thereof).

I think the way to go is to just change the environment variable for pwd, but when I do export PWD=/, it immediately looks like I'm in the root (anon@comp: / 32$) but when I do pwd or ls, it shows I'm still in my home directory.

Anything I am missing here?
 
Old 01-13-2013, 11:03 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
When you invoke,
Code:
~$ export PWD=/
It only changes the value of PWD environmental variable, but not your working directory.

If you will do:-
Code:
~$ echo $PWD
/
~$ pwd
/home/user
So there's difference between PWD environmental variable and pwd command.

To understand this, you have just stored some value in a variable i.e. PWD, and you can use that variable next time whenever you needed. But you have not changed your working directory.

In your case, when you do export PWD=/, it just stores your directory preference in PWD variable, but do not change your working directory. In order change your working dir. you will need to do:-
Code:
~$ cd $PWD
~$ pwd
/

Last edited by shivaa; 01-13-2013 at 11:13 PM.
 
1 members found this post helpful.
Old 01-13-2013, 11:08 PM   #3
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644
PWD is just an environment variable, it is updated when you traverse to a different directory. The reason your prompt changes is because it reads $PWD, but you haven't actually traversed anywhere and it will be reset when you do.

Does this count?
Code:
eval "cd /tmp"
I'm not aware of any other process but it wouldn't surprise me if there was ..
 
Old 01-14-2013, 12:05 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,946

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
Why would you want to 'cd without cd'? Because you're in a restricted shell? Then you should realize that it is called restricted for a reason.
 
Old 01-14-2013, 12:36 AM   #5
vitop
LQ Newbie
 
Registered: Jan 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Ok it makes sense that it is only changing the environment variable. I figured it was something like that. Does anyone know of a way that I could directly alter the value that is stored for current working directory? Surely cd doesn't have a monopoly on it.


Quote:
Originally Posted by NevemTeve View Post
Why would you want to 'cd without cd'? Because you're in a restricted shell? Then you should realize that it is called restricted for a reason.
No nefarious deeds at work here, just trying to get a better understanding of how the shell operates.
 
Old 01-14-2013, 12:46 AM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,653
Blog Entries: 2

Rep: Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095Reputation: 4095
Commands like cd, pushd/popd and pwd are in-built to the shell and directly influence the internals of the shell.
I think you are out of luck with trying to have any external influence on that.
 
1 members found this post helpful.
Old 01-14-2013, 12:54 AM   #7
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,032

Rep: Reputation: 309Reputation: 309Reputation: 309Reputation: 309
If you want to change directory without "cd" (or "chdir()", etc.) I'm afraid you're going to have to contact Yoda and ask about using The Force or something.
 
1 members found this post helpful.
Old 01-14-2013, 01:09 AM   #8
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
As I have said above, you have just stored "/" in PWD environmental variable, but it doesn't mean that your pwd i.e. present working directory has changed.

In order to change working directory, you will need to do, either:-
Code:
~$ cd /path/to/dir
Or, you can use PWD variable to change the directory, as:-
Code:
~$ cd $PWD
~$ pwd
/
Command cd is a built-in command and stands for change directory. So you will need it.

Moreover, if you want to change your working directory, when you login, then add following line in your .bashrc file:-
Code:
PWD=/
cd $PWD
Or you can create a simple text file like my_var.txt and add these line in that, and whenever you want to change your working dir. to the value stored in PWD, you can do it using without doing cd:-
Code:
~$ cat my_vars.txt
PWD=/
cd $PWD
~$ source my_vars.txt
~$ pwd
/

Last edited by shivaa; 01-14-2013 at 01:13 AM. Reason: Formatting
 
Old 01-14-2013, 01:13 AM   #9
vitop
LQ Newbie
 
Registered: Jan 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks guys these are all very helpful. I have a better idea of how the shell actually works now and appreciate the feedback.
 
Old 01-14-2013, 01:17 AM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
If you want to pretend to not use cd, you could alias it eg
Code:
alias go=cd
 
  


Reply


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
Can't Change Directories SANA4SPA Linux - Newbie 2 11-25-2011 11:48 PM
Need to change permissions on directories lyoumans Linux - Newbie 3 11-01-2009 04:41 AM
How do I change the color of directories? Echo Kilo Linux - Newbie 5 12-28-2007 01:40 AM
chmod to only change directories lluciano Linux - General 2 08-17-2005 03:14 PM
How to change directories in DOS zWaR General 17 11-09-2004 11:29 AM


All times are GMT -5. The time now is 11:37 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration