grep
from the root filesystem or you'll match and try to edit several thousand files.)
Expanding arithmetic expressions
Sometimes, you want to pass arithmetic results to a command. There are two forms that you can use to expand an arithmetic expression and pass it to the shell: $
[expression] or $
(expression). The following is an example:
$ echo "I am $[2020 - 1957] years old." I am 63 years old.
The shell interprets the arithmetic expression first (2020 - 1957
) and then passes that information to the echo
command. The echo
command displays the text with the results of the arithmetic (63
) inserted.
Here's an example of the other form:
$ echo "There are $(ls | wc -w) files in this directory." There are 14 files in this directory.
This lists the contents of the current directory (ls
) and runs the word count command to count the number of files found (wc -w
). The resulting number (14, in this case) is echoed back with the rest of the sentence shown.
Expanding variables
Variables that store information within the shell can be expanded using the dollar sign ($
) metacharacter. When you expand an environment variable on a command line, the value of the variable is printed instead of the variable name itself, as follows:
$ ls -l $BASH -rwxr-xr-x. 1 root root 1219248 Oct 12 17:59 /usr/bin/bash
Using $BASH
as an argument to ls -l
causes a long listing of the bash command to be printed.
Using Shell Variables
The shell itself stores information that may be useful to the user's shell session in what are called variables. Examples of variables include $SHELL
(which identifies the shell you are using), $PS1
(which defines your shell prompt), and $MAIL
(which identifies the location of your user's mailbox).
You can see all variables set for your current shell by typing the set
command. A subset of your local variables is referred to as environment variables. Environment variables are variables that are exported to any new shells opened from the current shell. Type env to see environment variables.
You can type echo $VALUE, where VALUE is replaced by the name of a particular environment variable you want to list. And because there are always multiple ways to do anything in Linux, you can also type declare to get a list of the current environment variables and their values along with a list of shell functions.
Besides those that you set yourself, system files set variables that store things such as locations of configuration files, mailboxes, and path directories. They can also store values for your shell prompts, the size of your history list, and type of operating system. You can refer to the value of any of those variables by preceding it with a dollar sign ($
) and placing it anywhere on a command line. For example:
$ echo $USER chris
This command prints the value of the USER
variable, which holds your username (chris
). Substitute any other value for USER
to print its value instead.
When you start a shell (by logging in via a virtual console or opening a Terminal window), many environment variables are already set. Table 3.5 shows some variables that are either set when you use a bash shell or that can be set by you to use with different features.
Creating and using aliases
Using the alias
command, you can effectively create a shortcut to any command and options that you want to run later. You can add and list aliases with the alias
command. Consider the following examples of using alias
from a bash shell:
$ alias p='pwd ; ls –CF' $ alias rm='rm -i'
In the first example, the letter p
is assigned to run the command pwd
and then to run ls -CF
to print the current working directory and list its contents in column form. The second example runs the rm
command with the -i
option each time you type rm
. (This is an alias that is often set automatically for the root user. Instead of just removing files, you are prompted for each individual file removal. This prevents you from automatically removing all of the files in a directory by mistakenly typing something such as rm *
.)
TABLE 3.5 Common Shell Environment Variables
Variable | Description |
BASH
|
This contains the full pathname of the bash command. This is usually /bin/bash .
|
BASH_VERSION
|
This is a number representing the current version of the bash command.
|
EUID
|
This is the effective user ID number of the current user. It is assigned when the shell starts, based on the user's entry in the /etc/passwd file.
|
FCEDIT
|
If set, this variable indicates the text editor used by the fc command to edit history commands. If this variable isn't set, the vi command is used.
|
HISTFILE
|
This is the location of your history file. It is typically located at $HOME/.bash_history .
|
HISTFILESIZE
|
This is the number of history entries that can be stored. After this number is reached, the oldest commands are discarded. The default value is 1000. |
HISTCMD
|
This returns the number of the current command in the history list.
|
HOME
|
This is your home directory. It is your current working directory each time you log in or type the cd command with any options.
|
HOSTTYPE
|
This is a value that describes the computer architecture on which the Linux system is running. For Intel-compatible PCs, the value is i386, i486, i586, i686, or something like i386-linux . For AMD 64-bit machines, the value is x86_64 .
|
MAIL
|
This is the location of your mailbox file. The file is typically your username in the /var/spool/mail directory .
|
OLDPWD
|
This is the directory that was the working directory before you changed to the current working directory. |