Shell Special Characters and Variables - Ubuntu Linux Toolbox: 1000+ Commands for Power Users (2013)

Ubuntu Linux Toolbox: 1000+ Commands for Power Users (2013)

Appendix B

Shell Special Characters and Variables

IN THIS APPENDIX

· Using special shell characters

· Using shell variables

Ubuntu provides bash as the default shell. Chapter 3 helps you become comfortable working in the shell. This appendix provides a reference to the numerous characters and variables that have special meaning to the bash shell. Many of those elements are referenced in Table B-1 (“Shell Special Characters”) and Table B-2 (“Shell Variables”).

Using Special Shell Characters

You can use special characters from the shell to match multiple files, save some keystrokes, or perform special operations. Table B-1 shows some shell special characters you may find useful.

Table B-1: Shell Special Characters

Character

Description

*

Match any string of characters.

?

Match any one character.

' ... '

Remove special meaning of characters between quotes. Variables are not expanded.

" ... "

Same as simple quotes except for the escape characters ($, `, and \) that preserve their special meaning. Variables are expanded.

\

Escape character to remove the special meaning of the character that follows.

~

Refers to the $HOME directory.

~+

Value of the shell variable PWD (working directory).

~-

Refers to the previous working directory.

.

Refers to the current working directory.

..

Refers to the directory above the current directory. Can be used repeatedly to reference several directories up.

$param

Used to expand a shell variable parameter.

cmd1 `cmd2` or cmd1 $(cmd2)

cmd2 is executed first. Then the output of cmd2 is used as input to cmd1.

cmd1> file

Redirects standard output from a command to a file.

cmd1< file

Redirects standard input from a file to a command.

cmd1>> file

Appends standard output to a file from a command, without erasing its current contents.

cmd1|cmd2

Pipes the standard output of one command to the input of the next.

cmd&

Runs the command in the background.

cmd1&&cmd2

Runs the first command; then if it returns a zero exit status (success), runs the second command.

cmd1||cmd2

Runs the first command; then, if it returns a non-zero exit status (not success), runs the second command.

cmd1 ;cmd2

Runs the first command and when it completes, runs the second command.

Using Shell Variables

You identify a string of characters as a variable by placing a $ in front of it (as in $HOME). Shell environment variables can hold information that is used by the shell itself, as well as by commands you run from the shell.

Many commands check for particular variables to be set. Not all of those variables will be populated by default. Some of these variables you can change (such as the default printer in $PRINTER or your command prompt in $PS1). Others are managed by the shell (such as $OLDPWD). Table B-2 contains a list of many useful shell variables.

Table B-2: Shell Variables

Shell Variable

Description

BASH

Shows path name of the bash command (/bin/bash).

BASH_COMMAND

The command that is being executed at the moment.

BASH_VERSION

The version number of the bash command.

COLORS

Path to the configuration file for ls colors.

COLUMNS

The width of the terminal line (in characters).

DISPLAY

Identifies the X display where commands launched from the current shell will be displayed (such as :0.0).

EUID

Effective user ID number of the current user. It is based on the user entry in /etc/passwd for the user that is logged in.

FCEDIT

Determines the text editor used by the fc command to edit history commands. The vi command is used by default.

GROUPS

Lists groups (by group ID) of which the current user is a member.

HISTCMD

Shows the current command’s history number.

HISTFILE

Shows the location of your history file (usually located at $HOME/.bash_history).

HISTFILESIZE

Total number of history entries that will be stored (default, 1000). Older commands are discarded after this number is reached.

HISTCMD

The number of the current command in the history list.

HOME

Location of the current user’s home directory. Typing the cd command with no options returns the shell to the home directory.

HOSTNAME

The current machine’s hostname.

HOSTTYPE

Contains the computer architecture on which the Linux system is running (i386, i486, i586, i686, x86_64, ppc, or ppc64).

LESSOPEN

Set to a command that converts content other than plain text (images, RPMs, zip files, and so on) so it can be piped through the less command.

LINES

Sets the number of lines in the current terminal.

LOGNAME

Holds the name of the current user.

LS_COLORS

Maps colors to file extensions to indicate the colors the ls command displays when encountering those file types.

MACHTYPE

Displays information about the machine architecture, company, and operating system (such as x86_64-pc-linux-gnu).

MAIL

Indicates the location of your mailbox file (typically the username in the /var/spool/mail directory).

MAILCHECK

Checks for mail in the number of seconds specified (default is 60).

OLDPWD

Directory that was the working directory before changing to the current working directory.

OSTYPE

Name identifying the current operating system (such as linux or linux-gnu).

PATH

Colon-separated list of directories used to locate commands that you type (/bin, /usr/bin, and $HOME/bin are usually in the PATH). Directories are searched from left to right.

PPID

Process ID of the command that started the current shell.

PRINTER

Sets the default printer, which is used by printing commands such as lpr and lpq.

PROMPT_COMMAND

Set to a command name to run that command each time before your shell prompt is displayed. (For example, PROMPT_COMMAND=ls lists commands in the current directory before showing the prompt).

PS1

Sets the shell prompt. Items in the prompt can include date, time, username, hostname, and others. Additional prompts can be set with PS2, PS3, and so on.

PWD

The directory assigned as your current directory.

RANDOM

Accessing this variable generates a random number between 0 and 32767.

SECONDS

The number of seconds since the shell was started.

SHELL

Contains the full path to the current shell.

SHELLOPTS

Lists enabled shell options (those set to on).