Environment variables

Let's call it a proof of concept more than a real means to have processes communicate with each other. Who would really want to mess with the environment? Anyway, we are exploring some viable means to IPC, so we can take this in account even though we will not use it in the first instance. Let's have a look at env:

env
LS_COLORS=REDACTED
XDG_MENU_PREFIX=xfce-
LANG=en_GB.utf8
DISPLAY=:0.0
XDG_VTNR=7
SSH_AUTH_SOCK=/tmp/ssh-MgHTC62oCYDp/agent.1121
GLADE_CATALOG_PATH=:
XDG_SESSION_ID=2
XDG_GREETER_DATA_DIR=/var/lib/lightdm/data/zarrelli
USER=zarrelli
GLADE_MODULE_PATH=:
DESKTOP_SESSION=xfce
PWD=/home/zarrelli
HOME=/home/zarrelli
GUAKE_TAB_UUID=b07321dd-a221-41bd-8ecc-0ae94b9082b9
SSH_AGENT_PID=1159
QT_ACCESSIBILITY=1
XDG_SESSION_TYPE=x11
XDG_DATA_DIRS=/usr/share/xfce4:/usr/local/share/:/usr/share/:/usr/share
XDG_SESSION_DESKTOP=xfce
GLADE_PIXMAP_PATH=:
GTK_MODULES=gail:atk-bridge
TERM=xterm
SHELL=/bin/bash
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
XDG_CURRENT_DESKTOP=XFCE
QT_LINUX_ACCESSIBILITY_ALWAYS_ON=1
SHLVL=1
XDG_SEAT=seat0
LANGUAGE=en_GB:en
GDMSESSION=xfce
LOGNAME=zarrelli
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
XDG_RUNTIME_DIR=/run/user/1000
XAUTHORITY=/home/zarrelli/.Xauthority
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0
XDG_CONFIG_DIRS=/etc/xdg
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
SESSION_MANAGER=local/moveaway:@/tmp/.ICE-unix/1169,unix/moveaway:/tmp/.ICE-unix/1169
OLDPWD=/home/zarrelli
_=/usr/bin/env

We trimmed out the content of the LS_COLORS variable, but even so, one outstanding issue is that the output is a bit crowded and holds a lot of information, most of it vital for our login session. So, first advice, let's be really cautious when tinkering with the environment variable.

One thing we must keep in mind is that there is a big difference between a shell variable and an environment one; let's see an example:

#!/bin/bash
a=10
b=20
echo -e "n"
echo "This is the value of a in the main subshell: $a"
(a=$((a+b)) ; echo "Inside the nested subshell a now has the value of: $a")
echo "Back to the main subshell a has a value of: $a"
echo -e "n"
echo "And now we will tinker with the environment..."
echo "This is the value of a in the main subshell: $a"
(
export a=$((a+b))
b=$((a+b))
echo "Inside the nested subshell a now has the value of: $a"
echo "Inside the nested subshell b now has the value of: $b"
echo "The value of the environment variable a is:"
env | grep ^a
echo "Here is the value of a at this level of subshell using process substitution:"
grep ^a <(env)
echo "And they are the same, since the nested shell share the environment variables of the parent shell"
echo "b is inherited as well: $b"
)
echo "Back to the main subshell the environment variable a has a value of: $a"
echo "Back to the main subshell the shell variable b has a value of: $b"

Executing it, we will get this:

zarrelli:~$ ./environment.sh 
This is the value of a in the main subshell: 10
Inside the nested subshell a now has the value of: 30
Back to the main subshell a has a value of: 10

And now we will tinker with the environment:

This is the value of a in the main subshell: 10
Inside the nested subshell a now has the value of: 30
Inside the nested subshell b now has the value of: 50
The value of the environment variable a is:
a=30
Here is the value of a at this level of subshell using process substitution:
a=30

And they are the same, since the nested shell shared the environment variables of the parent shell:

b is inherited as well: 50
Back to the main subshell the environment variable a has a value of: 10
Back to the main subshell the shell variable b has a value of: 20

What we get from this example concerns the normal use of variables. There is no real difference between shell and environment variables: both are accessible by sub processes/shells and both are unaffected by subshell manipulations. The real difference stands out when we have a subprocess which is executed by an execve() system call: in this case, the shell variable is not be passed through. We will have to export it to make it available to the subshell. If we want to have fun, there is something even trickier than this. A new keyword introduced with Bash 4.0 can reveal itself as a nice playground for our experiments.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.64.47