left-icon

Linux Succinctly®
by Jason Cannon

Previous
Chapter

of
A
A
A

CHAPTER 11

Additional Command Line Concepts

Additional Command Line Concepts


An environment variable is a name-value pair. Programs can use data from environment variables to determine how to behave in certain situations. For example, the default command used to display man pages can be altered by setting a value for the PAGER environment variable.

Environment variables are case-sensitive; however, by convention they are in all uppercase letters. To view the value of a known environment value, run echo $VAR_NAME or printenv VAR_NAME. You can use the env or printenv commands without arguments to display all the environment variables that are currently set.

$ echo $HOME

/home/jason

$ printenv HOME

/home/jason

$ printenv

SHELL=/bin/bash

TERM=xterm

USER=jason

MAIL=/var/mail/jason

PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

PWD=/home/jason

LANG=en_US.UTF-8

PS1=$

SHLVL=1

HOME=/home/jason

LOGNAME=jason

OLDPWD=/home/jason

$ env

SHELL=/bin/bash

TERM=xterm

USER=jason

MAIL=/var/mail/jason

PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

PWD=/home/jason

LANG=en_US.UTF-8

PS1=$

SHLVL=1

HOME=/home/jason

LOGNAME=jason

OLDPWD=/home/jason

$

When a process is launched, it inherits the exported environment variables of its parent process. An environment variable that is set only affects the current running process, unless it is explicitly exported. In the following example, the PAGER environment variable is set to less for the current shell. If a subprocess is started without that variable being exported, such as another instance of the Bash shell, that environment variable is not inherited. When PAGER is exported and a new Bash shell is started, it is available to that process. To remove an environment variable, use the unset command.

$ echo $PAGER

$ PAGER=less
$ echo $PAGER
less
$ bash
$ echo $PAGER

$ exit
exit
$ export PAGER=less
$ bash
$ echo $PAGER
less
$ exit
exit
$ echo $PAGER

less

$ unset PAGER

$ echo $PAGER


$

  1. Common Environment Variables

Use

Variable

The program used to edit files.

EDITOR

The user's home directory.

HOME

The user ID or login ID of the current user.

LOGNAME

The location of the user's mailbox on the local system.

MAIL

The old, or previous, working directory.

OLDPWD

The search path for commands.

PATH

The program used for paging through a file.

PAGER

The primary prompt string.

PS1

The present working directory.

PWD

The user ID or login ID of the current user.

USER

Aliases

You can use keyboard shortcuts, called aliases, at the command line. You can save yourself some time and typing by creating aliases for commands that you repeat often, that are long, that are hard to type, or that are difficult to remember. You can even use aliases to fix common typing mistakes. Some people even employ aliases to make Linux behave like another operating system they are familiar with.

alias [alias_name[=value]]   Without any arguments, the alias command lists the current aliases that are in your environment. Use alias alias_name=value to create a new alias.

unalias alias_name   Remove alias_name. Use unalias -a to delete all aliases.

$ alias ll='ls -l'

$ alias

alias ll='ls -l'

$ ls -l

total 32

drwxrwxr-x 2 jason jason 4096 Jun 21 22:01 Desktop

drwxrwxr-x 2 jason jason 4096 Jun 21 22:01 Documents

drwxrwxr-x 2 jason jason 4096 May 17 13:37 Downloads

-rw-rw-r-- 1 jason jason  274 Jun 28 14:52 goals.txt

drwxrwxr-x 3 jason jason 4096 Jun 21 22:05 Music

drwxrwxr-x 2 jason jason 4096 Jun 21 22:01 Pictures

-rw-rw-r-- 1 jason jason   73 Jun 29 02:30 to-do.txt

$ ll

total 32

drwxrwxr-x 2 jason jason 4096 Jun 21 22:01 Desktop

drwxrwxr-x 2 jason jason 4096 Jun 21 22:01 Documents

drwxrwxr-x 2 jason jason 4096 May 17 13:37 Downloads

-rw-rw-r-- 1 jason jason  274 Jun 28 14:52 goals.txt

drwxrwxr-x 3 jason jason 4096 Jun 21 22:05 Music

drwxrwxr-x 2 jason jason 4096 Jun 21 22:01 Pictures

-rw-rw-r-- 1 jason jason   73 Jun 29 02:30 to-do.txt

$ alias bu='/usr/local/bin/backup-database.sh'

$ bu

Starting database backup.

...

Database backup complete.

$ alias

alias bu='/usr/local/bin/backup-database.sh'

alias ll='ls -l'

$

Aliases only exist for your current session. So, if you were to create an alias, log out, and log in again, that alias would not be available. To make them persist between sessions, you have to add them to your personal initialization files.

Personal Initialization Files

To save customizations to your shell environment, place them in a personal initialization file. If you are using Bash, you can place your customizations in ~/.bashrc or ~/.bash_profile. The ~/.bash_profile file is read and executed for login sessions. When bash is not started as a login shell, for example when you open a new tab in your terminal emulator application, ~/.bashrc is read and executed. If you do not want or need this distinction, you can make ~/.bash_profile source ~/.bashrc and place all your customizations in ~/.bashrc. Using this method will provide the same environment whether it's a login shell or not. Personal initialization files are often referred to as "dot files."

$ cat ~/.bash_profile

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

$

The source command reads and executes the commands from the given file in the current shell. You can also source files by using a period, so source file1 and . file1 are the same thing. The if statement in the preceding example simply checks to see if the ~/.bashrc file exists before trying to source it.

$ echo "alias ll='ls -l'" >> ~/.bashrc

$ cat ~/.bashrc

# A line that begins with a pound sign is a comment.

# Place customizations in this file.

alias ll='ls -l'

$ . ~/.bashrc

$ alias ll

alias ll='ls -l'

$

Shell History

The commands that you execute at the command line are preserved in your shell history. Your history is retained in memory by Bash until your current session is ended. At that time, your history is saved to the ~/.bash_history file. Different shells save history in different files, but they usually include the word history and are stored as a dot file in your home directory. Also, you can set the HISTSIZE environment variable to control the number of commands to save in your shell history. The default value is 500.

history   When the history command is executed without arguments, it displays a list of commands in your shell history.

!N   Repeat the command associated with line number N.

!!   Repeat the previous command line.

!pattern   Repeat the most recent command starting with pattern.

$ history
1 ls
2 diff random-states random-states.bak
3 history
$ !1
ls
Desktop  Documents  Downloads  link-to-to-do  Music  program  tmp  to-do.txt
$ echo $SHELL
/bin/bash
$ !!
echo $SHELL
/bin/bash
$ !d
diff random-states random-states.bak

3c3

< Georgia        Atlanta

---

> Georgia        Savannah
$

You can search through your shell history by typing Ctrl-r. This starts a reverse search indicated by (reverse-i-search)`': and allows you to type in a portion of a command in your history to retrieve. To keep traversing your history for other commands that match your search pattern, continue to press Ctrl-r. Once you find a command you want to execute, press Enter. If you want to change the command line before executing it, press Esc. To completely abandon your reverse search, type Ctrl-c.

$ diff random-states random-states.bak

3c3

< Georgia        Atlanta

---

> Georgia        Savannah
(reverse-i-search)`di': diff random-states random-states.bak

3c3

< Georgia        Atlanta

---

> Georgia        Savannah

$

Tab Completion

To invoke tab completion, simply start typing a command and press the Tab key. Tab completion attempts to complete partially typed commands when possible. If there are many possibilities, those options can be displayed by pressing Tab twice. You can continue to type and press the Tab key again at any time.

In addition to completing commands, you can use tab completion to complete file and directory names. This can be useful when a file or directory is used as an argument to a command like ls, cat, rm, and others.

$ # Typing jo[Tab][Tab] results in:
$ jo

jobs  join


$ # Typing job[Tab][Enter] results in:
$ jobs
[1]+  Running                 ./db-backup.sh &

$ ls r*

random-states  random-states.bak

$ # Typing cat[Space]r[Tab][Enter] results in:

$ cat random-states

Tennessee        Nashville

Wyoming          Cheyenne

Indiana          Indianapolis

Indiana          Indianapolis

Arizona          Phoenix

Colorado         Denver

Indiana          Indianapolis

Georgia          Atlanta

$

Line Continuation

If you want to create a command line that visually spans multiple lines but acts as a single command, use a backslash at the end of each line you want to continue. When the backslash appears at end of a command line, it acts as the line continuation character. If you use this at the command prompt, the continued lines will be prefixed with the greater than symbol. You may encounter this when reading documentation or examining shell scripts.

$ diff \

> random-states \

> random-states.bak

3c3

< Georgia        Atlanta

---

> Georgia        Savannah
$ diff random-states random-states.bak

3c3

< Georgia        Atlanta

---

> Georgia        Savannah
$


Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.