LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: Changing $PWD from within a script (https://www.linuxquestions.org/questions/programming-9/bash-changing-%24pwd-from-within-a-script-4175440625/)

fatalerror0x00 12-08-2012 08:11 PM

Bash: Changing $PWD from within a script
 
I know this sounds kinda noobish maybe if it were me before now...I'm trying to make the variable $PWD the directory my script is in, if it's run through a symlink or from root and it's in the users home directory. This also assumes the location could change like the user downloads it so it's in there download folder then they decide to put it in a games folder. I need it to find out where it is and change $PWD as I will be using this variable often and so I need a global variable upon scripts and rather then delcaring my own why not use a system variable that already does what I need.

Well now I feel maybe I should make my variable global which will make things much more complicated now and would rather avoid it. I thought if you did "cd ~" in a script then echo $PWD it would be ~ (or any variant of that didn't wanna do any particular example) but apparently I thought wrong since if I run the script from / and I have startDir=$(echo $0) and then cd $startDir and then echo $PWD it's still / not in my case like I want it to be /home/shawn/Stephanie/startMenu/

any ideas how do change it and actually be in that directory. I've thought about just doing a PWD=/home/shawn/Stephanie/startMenu/ I really did but then I was sure if I did that if I would for one screw stuff up bad on my system and two if that would truly even work out the way I wish for it to work

fatalerror0x00 12-08-2012 08:41 PM

So apparently I've tried this again one last time and I actually got it to work :P I must have done something really wrong to screw up something so simple :P I'm sorry -.- for anyone who has replied to this if anyone (haven't reloaded page since I made post) Thank you thought

What I changed to is startDir="$(dirname "$(readlink -fn "$0")")" but I thought I tried that and was thinking yeah that doesn't work either but apparently I did something wrong once again :P

grail 12-09-2012 02:19 AM

I can see no reason why you need to alter PWD, simply set a variable to where you wish to be and use it.

fatalerror0x00 12-12-2012 08:21 PM

I never wanted to alter it I wanted to to change as i use cd in my programs to change directories but I did something wrong so I thought when I did that it wasn't changing but I messed up in the process but never wanted to alter it and did what you mentioned :) thanks

David the H. 12-16-2012 07:35 AM

To get the canonical location of a path, use readlink with the -e or -f options.

And since scripts run as sub-processes with their own environments, all you generally need to do is cd into the directory inside the script. When the script exits, you're back in the starting shell's environment.

Code:

#!/bin/bash

#get the canonical path of the script
scriptpath=$( readlink -qne "$0" )

#strip of the filename and cd into the directory
cd "${scriptpath%/*}"

#echo the current directory
echo "$PWD"

exit 0

If you need to operate on multiple directories within a script, you can run groups of commands in (..) subshells, which will act the same way.

Code:

for directory in "$HOME" /usr/bin /var/log ; do

        (
          cd "$directory"
          echo "$PWD"
        )

done

echo "$PWD"

Notice also the use of the built in $HOME variable instead of "~".

millgates 12-16-2012 10:47 AM

In addition to what has already been said, I would, if I may, suggest that you consider following the unix FHS. If you want to be a unix developer, think like one. So, you may want to put executables in "$prefix/bin/", data in "$prefix/share", user-specific data and configuration in "$HOME/.program", etc.


All times are GMT -5. The time now is 09:58 AM.