LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 01-28-2005, 07:51 AM   #1
slug420
Member
 
Registered: Jul 2004
Posts: 68

Rep: Reputation: 15
how to change PWD in a shell script


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?
 
Old 01-28-2005, 07:56 AM   #2
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Re: how to change PWD in a shell script

Quote:
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?
 
Old 01-28-2005, 07:59 AM   #3
slug420
Member
 
Registered: Jul 2004
Posts: 68

Original Poster
Rep: Reputation: 15
Re: Re: how to change PWD in a shell script

Quote:
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
 
Old 01-28-2005, 09:13 AM   #4
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
I think you'll find it hard (if not impossible) to do this from a shell script.

Have you taken a look at aliases or shell functions?

Groetjes,

Kees-Jan
 
Old 01-28-2005, 09:21 AM   #5
oneandoneis2
Senior Member
 
Registered: Nov 2003
Location: London, England
Distribution: Ubuntu
Posts: 1,460

Rep: Reputation: 48
PWD = Print working directory or PWD = password. . ?

To change the current directory, use cd. To change password from a script - don't!
 
Old 01-28-2005, 09:24 AM   #6
slug420
Member
 
Registered: Jul 2004
Posts: 68

Original Poster
Rep: Reputation: 15
pwd = print working directory...

cd does not work from within a script
 
Old 01-28-2005, 05:23 PM   #7
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
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.
 
Old 01-28-2005, 05:37 PM   #8
Blinker_Fluid
Member
 
Registered: Jul 2003
Location: Clinging to my guns and religion.
Posts: 683

Rep: Reputation: 63
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.
 
Old 01-29-2005, 07:27 AM   #9
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
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.

. /path/to/script
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change directory in a shell script? jdupre Linux - Newbie 13 06-25-2015 05:03 AM
Shell script to change password kamal_aitin Linux - General 6 07-25-2007 12:09 AM
Change batch script to shell script alan.belizario Programming 5 03-31-2005 12:41 AM
Change batch script to shell script alan.belizario Linux - Software 1 03-30-2005 01:49 AM
change user pwd in script? slug420 Linux - General 2 10-12-2004 10:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:09 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
Open Source Consulting | Domain Registration