Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I can save the IP address of the current server to a variable and echo it out.

# myvar=$(/sbin/ifconfig  | grep 'inet addr:'| grep -v '' | cut -d: -f2 | awk '{ print $1}')

# echo $myvar

What I want to do is to "export" it as a global variable so that I can use it in shell scripts/ other commands.

share|improve this question
up vote 1 down vote accepted

Then export it.

export varname="value"

This will be available afterwards (exported into the environment).


export $varname

If you want this globally for every shell upon login, you can put it into /etc/profile or something similar.

share|improve this answer

The answer depends on the shell you are using:

  • for sh-compatible shells (including bash) use: VARIABLE=value; export VARIABLE or just export VARIABLE=value
  • for tcsh: setenv VARIABLE value
  • for zsh: export VARIABLE=value
share|improve this answer

Just source it from other shell scripts:

source /path/to/
echo $myvar


. /path/to/
echo $myvar
share|improve this answer

For login shells you can set the variable globally in /etc/profile. Edit the file and add the following lines just after the export PATH ... line:

myvar=$(/sbin/ifconfig  | grep 'inet addr:'| grep -v '' | cut -d: -f2 | awk '{ print $1}')
export myvar
share|improve this answer

Your Answer


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.