Apr 11 2007

LCoTD: The Shell (Part 2: Shell Variables, Search Path, and Aliases)

Published by georgegumpert at 12:11 pm under Linux Command of the Day, Linux Resources, Series ()

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Today we’ll be covering shell variables, search path, and aliases.

Shell Variables

Variables serve a number of purposes in programming environments, as well as Linux. You can use them to store data that will be called later, and come in handy when you’re not sure what the value will be, but still want to setup a macro to run certain behaviors.

You can define variables and their values by assigning them:

$ MYVAR = 3

To refer to a value, simply place a dollar sign in front of the variable name:

$ echo $MYVAR
3

Some variables are standard and commonly defined by your shell upon login.

Variable Meaning
DISPLAY The name of your X window display

HOME

The name of your home directory
LOGNAME Your login name

MAIL

Path to your incoming mailbox
OLDPWD Your shell’s previous directory
PATH Your shell search path: directories seperated by colons
PWD Your shell’s current directory
SHELL The path to your shell, e.g., /bin/bash
TERM The type of your terminal, e.g., xterm or vt100
USER Your login name

To see a shell’s variables, run:

$ printenv

The scope of the variable (i.e., which programs know about it) is, by default, the shell in which it’s defined. To make a variable and its value available to other programs your shell invokes (i.e., subshells), use the export command:

$ export MYVAR

or the shorthand:

$ export MYVAR=3

Your variable is now called an environment variable, since it’s available to other programs in your shell’s “environment.” To make a specific value available to a specific program just once, prepend variable=value to the command line:

$ echo $HOME
/home/george
$ HOME=/home/sally echo “My home is $HOME”
My home is /home/sally
$ echo $HOME
/home/george

As you can see, the original value is unaffected.

Search Path

A very important variable is PATH, which instructs the shell where to find programs. When you type any command:

$ who

the shell has to find the program(s) in question. It consults the value of PATH, which is a sequence of directories separated by colons:

$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/george/bin

and looks for the who command in each of these directories. If it finds who (say, /usr/bin/who), it runs the command. Otherwise, it reports:

bash: who: command not found

To add directories to your shell’s search path temporarily, modify its PATH variable. For example, to append /usr/sbin to your shell’s search path:

$ PATH=$PATH:/usr/sbin
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/george/bin:/usr/sbin

To make this change permanent, modify the PATH variable in your startup file ~/.bash_profile, then log out and log back in.

Aliases

The built-in command alias defines a convenient shorthand for a longer command, to save typing. For example:

$ alias ll='ls -l'

defines a new command ll that runs ls -l:

$ ll
total 436
-rw-r--r-- 1 george 3584 Oct 11 14:59 file1
-rwxr-xr-x 1 george 72 Aug 6 23:04 file2
...

Define aliases in your ~/.bashrc file to be available whenever you log in. To see all your aliases, type alias. If aliases don’t seem powerful enough for you (since they have no parameters or branching), run info bash, and read up on “shell functions”.

That’s it for today. Tomorrow, I’ll cover Input/output redirection, Pipes, Combining commands, Quoting, Escaping, Command-line editing, Command history, and Filename completion. There will be a lot of content covered tomorrow, so be sure not to miss it!

Sphere: Related Content

If you found this article useful and use StumbleUpon, please give it a thumbs up so more people can read it! Thank you!

Please take the time to check out this thread and leave a comment letting me know what you would like to see from this site. It's still relatively young and trying to find its way- you can make design pitstop the resource you always wanted!

Leave a Reply