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.

link|improve this question

feedback

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.

link|improve this answer
feedback

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
link|improve this answer
feedback

Just source it from other shell scripts:

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

or:

. /path/to/ip.sh
echo $myvar
link|improve this answer
feedback

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
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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