Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')

# echo $myvar
10.11.6.117

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
add comment

4 Answers

up vote 1 down vote accepted

Then export it.

export varname="value"

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

Alternative:

varname="value"
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
add comment

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
add comment

Just source it from other shell scripts:

source /path/to/ip.sh
echo $myvar

or:

. /path/to/ip.sh
echo $myvar
share|improve this answer
add comment

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 '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
export myvar
share|improve this answer
add comment

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.