1834149 Members
2158 Online
110064 Solutions
New Discussion

Environment Variables

 
SOLVED
Go to solution
Din_1
Frequent Advisor

Environment Variables

Dear Gurus,

whats the difference between ` & ' in variables & how do i set my shell prompt to show hostname and present working directory.

Thanks in advance
Dinesh
7 REPLIES 7
Fat Scrape
Honored Contributor

Re: Environment Variables

Din,

insert this line in your .profile

PS1=`hostname`:'$PWD'#
export PS1

show hostname and present working directory

Fat
Fat Scrape
Honored Contributor

Re: Environment Variables

Dinesh,


For more information about shell and vars. anv see this guide:

http://www.phys.ualberta.ca/~gingrich/research/shells/shells.html

Fat
Din_1
Frequent Advisor

Re: Environment Variables

Hi Fat,

Thanks for your reply. I want to know when commands inserted between the ` characters its being executed and we are getting the output. As well as why the same character is not being used to get the output of PWD in this location.

Rgds,
Din
Fat Scrape
Honored Contributor
Solution

Re: Environment Variables

Dinesh,

in a shell script you can capture the output of the command in these ways:

VAR=`hostname`
or
VAR=$(hostname)

In a command script you use a command between `hostname` the shell try to exec not the command but its output

From command line try to exec `hostname` and see what error appears.

For create a PS1 var, in your case, must be execute a command to show the hostname, and an env var to show the current directory.

PS1=`hostname`:'$PWD'#

Fat



john korterman
Honored Contributor

Re: Environment Variables

Hi Dinesh,

nowadays you will often see the newer standard:
$(command)
instead of
`command`

However, the result is the same; commands in between are always executed/processed, e.g.

$ echo $(date +%a)
or
$ echo `date +%a`

in the example the date command is executed first and the echo command then displays the result.


In contrast, you can display the current value of a variable by just echoing it, e.g.:

echo "$PWD"

notice the use of double quotes, as one significant difference between single and double quotes is that any character in between single quotes is taken literally, e.g.:

echo '$PWD'


and a bit simplified you can say that the difference between single/double quotes and back quotes/$() is that using single/double quotes does not impose any processing on the string in between; you simply display the current content.
But the back quoutes/$() causes command execution of whatever in between.

However, PWD is a special variable because the shell supplies the content of it; the content is updated every time you change directory, e.g.:
$ echo $PWD
/home/john


you can change the content
$ PWD="hoooops"
$ echo $PWD
hoooops

but the shell will overrule it when you change directory

$ cd
echo $PWD
/home/john

So what may be confusing in contrast to a "non reserved, home made" variable is that the content of PWD is automatically updated by the shell.

regards,
John K.
it would be nice if you always got a second chance
Din_1
Frequent Advisor

Re: Environment Variables

Thank you very much friends,

I was having bit confusion on this subject. Now its got cleared.

Thanks
Dinesh
Din_1
Frequent Advisor

Re: Environment Variables

Thank you very much friends,