Your environment can affect you. If it’s raining and you’re sitting outside, well, then, you’re wet. Sorry about that. If you’re sitting inside with the air conditioning on, then you’re cool. Turning on the air conditioning affects your environment, turning it off can make you warm again. Bash has the same thing, you can turn things on and off again using environment variables.
They’re called variables because they can vary. How’s that for a spiffy name? Setting environment variables changes the behavior of your terminal session, they can also contain information about your terminal session as well.
They’re set by using the command export. It looks like:
Alpha:~ computerlamp$ export VAR=VALUE
If you want to see all the environment variables in your session, just do:
Alpha:~ computerlamp$ export declare -x G_BROKEN_FILENAMES="1" declare -x HISTSIZE="1000" declare -x HOME="/home/computerlamp" declare -x HOSTNAME="Alpha" declare -x INPUTRC="/etc/inputrc" declare -x LANG="en_US.UTF-8" declare -x LESSOPEN="|/usr/bin/lesspipe.sh %s" declare -x LOGNAME="computerlamp" declare -x LS_COLORS="" declare -x MAIL="/var/spool/mail/computerlamp" declare -x OLDPWD declare -x PATH="/usr/local/bin:/bin:/usr/bin:/home/computerlamp/bin" declare -x PWD="/home/computerlamp" declare -x SHELL="/bin/bash" declare -x SHLVL="1" declare -x TERM="xterm-256color" declare -x USER="computerlamp"
Let’s talk about a few of those from the list.
- USER . That’s my username! Hi, I’m computerlamp.
- PWD My current directory
- OLDPWD My previous directory. There’s nothing set there because I haven’t done anything in this login aside from sit in my current directory
- HOME My home directory. It’s the same as PWD right now, but it could be different. It’s always set to my home directory.
- SHELL The shell I’m using, right now I’m using /bin/bash
- HOSTNAME That’s the hostname of this computer, which happens to be Alpha.
Now let’s talk about that PATH variable. It’s several directories strung together with colons (:). The path is all the directories that bash looks in when you want to execute a command. My list is:
- /usr/local/bin/
- /bin/
- /usr/bin/
- /home/computerlamp/bin
When I try to run any command, bash starts by looking in /usr/local/bin/ for the command, then /bin/, next is /usr/bin/, and finally it tries /home/computerlamp/bin. If it can’t find it in any of those, it gives up and tells you that command isn’t available.
Now suppose I have two commands, /bin/Avenge and /usr/bin/Avenge. One of them calls Tony Stark and the next calls Steve Rogers.
Of course, at the end of Captain America: Civil War, they are not getting along.
If I just run Avenge, it’s going to call Tony Stark by default. If I want to call Steve Rogers, I’ll execute /usr/bin/Avenge.