LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-08-2012, 09:11 PM   #1
fatalerror0x00
Member
 
Registered: Oct 2012
Location: Holden, ME, USA
Distribution: SlackWare64 14.00
Posts: 185

Rep: Reputation: 0
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
 
Old 12-08-2012, 09:41 PM   #2
fatalerror0x00
Member
 
Registered: Oct 2012
Location: Holden, ME, USA
Distribution: SlackWare64 14.00
Posts: 185

Original Poster
Rep: Reputation: 0
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

Last edited by fatalerror0x00; 12-08-2012 at 09:42 PM.
 
Old 12-09-2012, 03:19 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
I can see no reason why you need to alter PWD, simply set a variable to where you wish to be and use it.
 
Old 12-12-2012, 09:21 PM   #4
fatalerror0x00
Member
 
Registered: Oct 2012
Location: Holden, ME, USA
Distribution: SlackWare64 14.00
Posts: 185

Original Poster
Rep: Reputation: 0
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
 
Old 12-16-2012, 08:35 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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 "~".

Last edited by David the H.; 12-16-2012 at 08:37 AM. Reason: minor code correction
 
Old 12-16-2012, 11:47 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Changing directory with a script (bash) markush Programming 5 09-10-2012 03:24 PM
[SOLVED] Bash Script combination of sed with pwd command escaping slashes HaukeG Programming 2 10-06-2009 05:04 AM
Overriding/Changing Root Pwd for Mandrake 4.0 with Lilo Rohi Linux - Newbie 1 07-05-2009 11:07 AM
changing a users pwd ALInux Linux - Newbie 1 11-07-2005 01:44 PM
Changing behavior of pwd with symlinks rose_bud4201 Linux - Software 2 02-17-2005 04:46 PM


All times are GMT -5. The time now is 06:44 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration