LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-21-2013, 02:48 PM   #1
ulkoma
Member
 
Registered: Feb 2012
Distribution: CentOS
Posts: 69

Rep: Reputation: Disabled
Does changing environment variables always reflect on the shell?


I mean how come when I change the prompt variables the command prompts changes but if I try to change $PWD the shell does not change the directory I am in?!

Thanks
 
Old 05-21-2013, 03:10 PM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,476

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
${{PWD} is a shell built-in that, when you execute something, is updated; e.g., change directory and it's immediately updated to indicate the current working directory.

So, nope, can't change that (and have it be effective).

Here's an example of setting the prompt to show the host name, user name and current working directory:
Code:
PS1='${HOST}-${USER}-${PWD}: '
(some systems require this prior to setting PS1, some don't, but this will always do it).
Code:
# Set the HOST environment variable
export HOST="`uname -n`"
Hope this helps some.

Last edited by tronayne; 05-21-2013 at 03:12 PM.
 
1 members found this post helpful.
Old 05-21-2013, 03:19 PM   #3
ulkoma
Member
 
Registered: Feb 2012
Distribution: CentOS
Posts: 69

Original Poster
Rep: Reputation: Disabled
ok so how do I know which variables are read only and which variables I can manipulate?
 
Old 05-21-2013, 05:37 PM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,476

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
Depends -- just which variables do you want to manipulate?

You can make up your own shell variables when writing shell programs (scripts), avoiding the built ins. Typically you define variables in shell programs in upper case (VARIABLE=somevalue) then refer to that variable with ${VARIABLE}.

You can find a list of the built-ins at http://answers.oreilly.com/topic/149...ell-variables/.

Hope this helps some.
 
1 members found this post helpful.
Old 05-22-2013, 11:01 AM   #5
ulkoma
Member
 
Registered: Feb 2012
Distribution: CentOS
Posts: 69

Original Poster
Rep: Reputation: Disabled
Nice link, good to know that some variables are read only! however pwd is not among them
 
Old 05-22-2013, 11:31 AM   #6
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,476

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
Quote:
OLDPWD Previous working directory (set by cd).
PWD Current working directory (set by cd).
Mess with them at your peril.
 
Old 05-24-2013, 08:59 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
PWD and the OLDPWD only keep track of the current and previous directories, they don't actually change anything themselves. They're for information only. But they have to be writable or else the shell wouldn't be able to update them when you use cd/pushd/popd to change directories.

Start by reading the bash manpage (or the documentation of whatever shell you use) to see what built-in variables there are, what they do, and what they are set to by default. The bash built-in declare -p will also list all current variables and what flags are set on them, along with their current values. The manpage entry for declare lists all possible flags.

Environment variables are nearly always upper-case only, by the way, and it's usually a good idea to use some other naming pattern (e.g. lower or mixed case) to differentiate your own user variables from them.

Scripting With Style
 
  


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
how to access environment variables in shell script jayasekar Programming 4 07-30-2011 12:30 AM
exporting makefile variables to $(shell) environment ta0kira Programming 4 05-14-2010 01:45 AM
global environment variables in shell script a.toraby Programming 2 11-11-2007 04:25 AM
changing environment variables. Geminias Linux - Newbie 2 01-04-2006 07:02 PM
Sub shell environment variables tnine9 Programming 1 06-08-2004 02:27 PM


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