[plug] Changing working directory from a bash script

Bernd Felsche bernie at innovative.iinet.net.au
Mon Jan 22 16:04:10 WST 2007


"Patrick Coleman" <blinken at gmail.com> writes:

>I realise that this seems pretty basic, but I'm trying to change the
>working directory from within a bash shell script and not having much
>luck.

They're not scripts. They are programs!

>Here's what I want to do:

>restore3:~ admin$ ./somescript.sh
>[script runs]
>restore3:/some/directory admin$

>I've tried cd from within the script, but that evidently doesn't work
>because the script is executed in a subshell. I've also tried 'export
>PWD=$dir' with no luck. Obviously both of these work when executed in
>a normal shell, but not from a script.

>Any suggestions would be much appreciated.

No process can change the working directory of another.

To make a shell change directories based on commands in a file, you
need to "source" the commands into that shell.

i.e.
	restore3:~ admin$ . ./somescript.sh

Note the leading dot, all by its lonesome. It's a shorthand for
"source". You can use either but there's less opportunity to
mis-type "."

-- 
/"\ Bernd Felsche - Innovative Reckoning, Perth, Western Australia
\ /  ASCII ribbon campaign | "If we let things terrify us,
 X   against HTML mail     |  life will not be worth living."
/ \  and postings          | Lucius Annaeus Seneca, c. 4BC - 65AD.




More information about the plug mailing list