-1

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.

HopelessN00b
  • 53,795
  • 33
  • 135
  • 209
shantanuo
  • 3,579
  • 8
  • 49
  • 66

4 Answers4

2

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.

Sven
  • 98,649
  • 14
  • 180
  • 226
2

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
Vladimir Blaskov
  • 6,183
  • 1
  • 27
  • 22
1

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
ciupinet
  • 142
  • 3
0

Just source it from other shell scripts:

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

or:

. /path/to/ip.sh
echo $myvar
quanta
  • 51,413
  • 19
  • 159
  • 217