Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
Originally posted by slug420 how can I change the current directory of the user from within a script?
I want the user to log in and be in their home directory but if they run the script for it to change their path to /etc or something.
If I use cd in the script or set the PWD variable it only affects the script as it runs and when it is finished the user is still in their home dir.
how can I do this?
Simply use cd. And not sure why but why would you need their path to get changed to /etc? Why not just use the current command passwd which they can invoke anywhere to change their password?
Originally posted by trickykid Simply use cd. And not sure why but why would you need their path to get changed to /etc? Why not just use the current command passwd which they can invoke anywhere to change their password?
Quote:
Originally posted by slug420 If I use cd in the script or set the PWD variable it only affects the script as it runs and when it is finished the user is still in their home dir.
/etc is only an example of a different directory. call it /directory/i/want/the/user/to/be/in/is/irellevant if you prefer
Yes, cd does work within a script, the shell which runs the script spawns a sub-shell, /bin/bash, and within that shell you can cd wherever you want. When the script ends it will dump you back into the shell you started from which will still have the same working directory.
It's cheezy but it might just work...
create your script to do whatever your script does then set up and alias like so:
alias run_me='cd /home/user_username/spam/foo ; /home/user_name/spam/script'
Then when you run the run_me alias it will cd to the directory and run the script.
If that's too cheezy find your own way to do it.
When you run a script it is run from a new shell environment. Any directory change will only effect that shell environment, and not the parent. If you would source the script then it is run from within the parent environment and then the cd command would effect the current environment.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.