[SOLVED] Change directories without cd? (or a variant)
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:-
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 ..
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
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.
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.
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.
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:-
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.