Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

It's a quite simple asked question I think: What are the key differences between using environment variables just like $HOSTNAME and `hostname`. Where is which appropriate, why are there two possibillities to represent them?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

An environment variable is set once and for all (for a given process; when a process launches another, the child inherits the parent's environment). So accessing $HOSTNAME directly reads a value in memory. In contrast, each time you evaluate the command substitution `hostname` (also spelled $(hostname)), this executes the hostname command.

In the case of $HOSTNAME and hostname, in practice, it doesn't make any difference. $HOSTNAME is marginally faster. In principle, $HOSTNAME can be wrong if you've run HOSTNAME=some_bogus_value (whereas $(hostname) can be wrong if you put some different hostname program on your $PATH), and only hostname keeps up-to-date if you change your host name.

There's often (but not always) a HOSTNAME environment variable because it's often used (e.g. in shell prompts) and almost never changes. There's a hostname command (on most systems) to set the value of HOSTNAME (HOSTNAME=$(hostname)) and to change the host name (hostname $(cat /etc/hostname), typically done early in the boot process).

share|improve this answer
    
Gilles is just too good to be simple!! I just wish those Bronze, Gold, Silver badges, as many as you sport, on my name. ;-) –  Nikhil Mulley Jan 4 '12 at 15:30

It is appropriate to depend on the commands which fetch the current value than the variables which can get stale over a period of time in the unattended sessions.

[centos@abc ~]$ /bin/hostname
abc.dns.com
[centos@abc ~]$ echo $HOSTNAME
abc.dns.com
[centos@abc ~]$ uname -n
abc.dns.com

[centos@abc ~]$ sudo /bin/hostname xyz.dns.coom
[centos@abc ~]$ uname -n
xyz.dns.coom
[centos@abc ~]$ /bin/hostname
xyz.dns.coom
[centos@abc ~]$ echo $HOSTNAME
abc.dns.com
[centos@abc ~]$ 

I think its clear that $HOSTNAME is a shell variable, which is expanded/evaluated by capturing the output of /bin/hostname at the start of the shell initialization and its not updated everytime or if the hostname is dynamic. Depending on the output of /bin/hostname is more reliable than $HOSTNAME unless it is the same script or program that has already evaluated the name of the host in the same program space.

Well, in short, output of the commands /bin/hostname or /bin/uname -m is more appropriate method to know the hostname than just using $HOSTNAME (which could become stale as any other normal SHELL variable).

If you are curious to know how does shell gets $HOSTNAME inherited, then its proper to know that shell reads its global initialization (rc) files like /etc/profile (or /etc/bashrc) which has this variable defined from again output of /bin/hostname .

(It is also important to know that programs other than shell without prior executing /bin/hostname command can have HOSTNAME variable defined/initialized because of the system startup script called /etc/rc.sysinit )

share|improve this answer
    
Good explanation. Can you include a comparision of hostname to $HOSTNAME and the use of an absolute path? –  Michael Jan 3 '12 at 22:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.