LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 08: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, 08: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 08:42 PM.
 
Old 12-09-2012, 02:19 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 08: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, 07:35 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 07:37 AM. Reason: minor code correction
 
Old 12-16-2012, 10:47 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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



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 02:24 PM
[SOLVED] Bash Script combination of sed with pwd command escaping slashes HaukeG Programming 2 10-06-2009 04:04 AM
Overriding/Changing Root Pwd for Mandrake 4.0 with Lilo Rohi Linux - Newbie 1 07-05-2009 10:07 AM
changing a users pwd ALInux Linux - Newbie 1 11-07-2005 12:44 PM
Changing behavior of pwd with symlinks rose_bud4201 Linux - Software 2 02-17-2005 03:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:08 AM.

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