Exploring the 04 bash shell in Linux

Posted by AlanG on Tue, 25 Jan 2022 18:27:36 +0100

-----Latest update [December 30, 2021]-----

Preview of this article's directory structure:

  • 1, Introduction
  • 2, shell variable
    1. View variables
    2. Variable type
    3. Variable operation
    4. Common global variables in the system
  • 3, shell options
    1. View shell options
    2. Set shell options
  • 4, Metacharacter
    1. Metacharacter list
    2. Citation and escape
  • 5, shell built-in commands
    1. View description
    2. Common built-in commands
  • 6, Search path
    1. View search path
    2. Modify search path
  • 7, History list
    1. View history list
    2. Call history command
    3. Retrieve and modify historical commands
    4. Search history command
    5. Set history list size
  • 8, alias
    1. Grammar
    2. Create alias
    3. View alias
    4. Remove alias
    5. Do not use aliases
  • 9, Initialization file
    1. File name
    2. Login shell and non login shell
    3. Initialization file content
  • 10, Reference

1, Introduction

Simply put, a shell is a Unix program that acts as a user interface and script interpreter, allowing users to enter commands and indirectly access the services of the kernel.

In terms of function:
First, a shell is a program that reads and interprets the commands entered. Every time a user enters a Unix command, the shell reads the command and points out what to do, so the shell is a command processor.
Second, the shell also supports some types of programming languages. Using this language, you can write programs interpreted by the shell, which are called shell scripts.

At present, there are several popular shells: Bash, Korn shell, C-Shell and Tcsh.

Bash is the most popular shell at present. This article also takes bash as the environment.

If you don't know what kind of shell you are currently using, you can check it with the command echo $SHELL.

There may be many contents in this article. In order to know the contents in advance, here is a brief list:

  • shell variable
    --View variables, variable types, variable operations, and common global variables of the system
  • shell options
    --View shell options, set shell options
  • Metacharacter
    --Metacharacter list, reference and escape
  • shell built-in commands
    --View description, common built-in commands
  • Search path
    --View search path and modify search path
  • History list
    --View history list, retrieve history commands, retrieve and modify history commands, search history commands, and set the size of history list
  • alias
    --Syntax, create alias, view alias, remove alias
  • Initialization file
    --File name, login shell and non login shell, initialization file content

2, shell variable

1. View variables

The above mentioned $shell is a global variable of the shell, and there are many other variables in the shell. You can use the command env or printenv to view global variables.

Using the set command without options or parameters can also display all shell variables and their values.

2. Variable type

Depending on the storage type, shell variables almost always store one type of data, that is, strings.

According to the scope of variables, shell variables can be divided into the following three types:

  • Some variables can be used in the current shell process and its child processes, which are called global variables
  • Some variables can only be used in the current shell process, which is called environment variables
  • Some variables can only be used inside a function, which is called a local variable (the scope is only a code fragment (function context))

If you use the export command to export the environment variable, it can also be used in all child processes. At this time, it is called "global variable".

Many books or websites have vague definitions of environment variables and global variables. I use a better way to define them here.

3. Variable operation

Variables usually have four different types of operations: create variables, view variables, modify variables, and delete variables.

The creation of variables is very simple. Just use the following syntax:

Variable name=Variable value  # be careful! There must be no spaces before or after the equal sign.

Delete variable:

unset Variable name ...

Example:

[14:59 linux1@noseeu ~]$ MYNAME=Nosee    #Create variables (currently shell available)
[14:59 linux1@noseeu ~]$ echo $MYNAME    #View variables
Nosee
[14:59 linux1@noseeu ~]$ export $MYNAME     #Export variables (current shell and child processes are available)
[15:00 linux1@noseeu ~]$ unset MYNAME    #Delete variable
[15:00 linux1@noseeu ~]$ echo $MYNAME

[15:00 linux1@noseeu ~]$ 

be careful:
The variables exported through export are only valid for the current Shell process and all child processes. If the top-level parent process is closed, the global variable will disappear and other processes will not be able to use it. This variable is only temporary.
The permanent purpose can be achieved only by writing the variable into the Shell configuration file. The Shell process will execute the code in the configuration file and do some initialization every time it starts. If the variable is placed in the configuration file, the variable will be defined every time the process starts.

4. Common global variables in the system

HISTFILE # History list: the file name used to store historical commands
HISTSIZE # History list: the maximum number of commands used to store history
HOME # home directory
HOSTNAME # Computer name
LOGNAME # Current user flag (user name)
PATH # Program search directory
PS1 # shell prompt
PWD # Current working directory
SHELL # shell type
TERM # Terminal type
USER # Current user flag (user name)

3, shell options

In Bash shell, when we need to control all aspects of shell behavior, we can use shell options.

The shell option is like an on/off switch. When an option is opened, it is said that this option is set; When this option is turned off, it is said that this option is reset.

shell options are either off or on, and they do not need to be created.

1. View shell options

You can use the command set -o or set +o to view all shell options. The two viewing methods are just different in display format.

The set -o command is displayed in an easy to read manner (human readable)
The output displayed by the set +o command is suitable for use as data for shell scripts (machine readable)

Description of options:

option Short name meaning
allexport -a Export all subsequently defined variables and functions
braceexpand -B Enable bracket extension (generate character mode)
emacs Command line editor: Emacs mode, turn off vi mode
hashall -h Command hash location when the command is found (remember)
hisexpand -H History list: enabled! Style replacement
history History list: Enable
ignoreeof Ignore eof signal ^ D; Use exit or logout to exit the shell
minitor -m Job control: Enable
noclobber -C Replacing a file with redirected output is not allowed
notify -b Job control: notify immediately when the background job ends
vi vi mode: process each typed character immediately

You can see more shell options with the command shoot.

2. Set shell options

Syntax:

set -o option  # Open shell option (on)
set +o option  #Reset shell option (off)

Note that - o here means to turn on an option and + o means to turn off an option.

1) For example, I want to turn on an option (the option noclobber means that redirected output is not allowed to replace a file)
You can:

set -o noclobber # Turn this option on

Or:

set -C

If this option is turned off, it will be like this: set +o noclobber or set +C

2) Ignore eof signal ^ D

[21:34 linux1@noseeu ~]$ set -o ignoreeof
# At this point, I press the < CRLT > - D key combination again
[21:34 linux1@noseeu ~]$ Use "logout" to leave the shell.

Programmers who design shells know how people use them, so in most cases, the default shell option meets the requirements. This means that we rarely need to modify the shell options from the cloud.

4, Metacharacter

In the shell, there are many characters with special meanings. We call such characters metacharacters. If; (semicolon), \ (backslash) (DOT), abstract keys such as < space >, < tab >, < ENTER > also use metacharacters.

1. Metacharacter list

character name effect
| The Conduit Command line: creating a pipe line
< less than Command line: redirecting input
> greater than Command line: redirecting output
() Parentheses Command line: running commands in a subshell
# hash,pound Command line: Comments
; semicolon Command line: used to separate multiple commands
` backquote Command line: Command substitution
~ Wave sign File name extension: inserts the name of the home directory
? question mark File name extension: match any character
[] square brackets File name extension: matches characters in a set of characters
* asterisk File name extension: matches 0 or more characters
! Exclamation mark, bang History lists: event markers
& Sum number Job control: running commands in the background
\ Backslash Reference: next character escape
' Single quotation mark References: canceling all substitutions
" Double quotation mark References: canceling most substitutions
{} Curly bracket Variable: determines the bounds of variable names
$ Dollar sign Variable: replace with the value of the variable
Space character Whitespace: separates words on the command line
Tab Whitespace: separates words on the command line
/ New line character Blank: marks the end of a line

The above list of metacharacters shows the common functions of metacharacters in the shell.

2. Citation and escape

Sometimes, when we want to use metacharacters literally (rather than using their special meaning), we have to tell the shell to interpret the characters literally. When you do this, you can call it a reference character.
There are three ways to quote characters: using backslashes, a pair of single quotes, or a pair of double quotes.

When a backslash is used to refer to a single character, we call the backslash an "escape character".

For example:

echo It is warm\; come on.

In the above example,; (semicolon) has a special meaning in the shell. If we want to output as is, we must escape. At this time, you can say "escaped the semicolon with a backslash", or you can say "referenced the semicolon with a backslash".

When there are few metacharacters, the above method has no problem. But when there are many metacharacters, we need to use single quotation marks or double quotation marks.

1) Strong reference (single quotation mark)
All metacharacters in single quotation marks will be escaped, that is, all metacharacters will be output as is.

For example, the following $will be escaped and $NAME will be output as it is:

linux1@noseeu:~$ echo 'My name is $NAME'
My name is $NAME

2) Weak reference (double quotation mark)
When quoting in double quotation marks, the special meaning of $(dollar sign), ` (back quotation mark) and \ (back slash) will be retained.

For example:

linux1@noseeu:~$ echo "My name is $NAME"
My name is Chan
[22:04 linux1@noseeu ~]$ echo "now is `date`."
now is Sun 26 Dec 2021 10:04:24 PM UTC.

The above $will not be escaped, and $NAME will be replaced with the actual value of NAME` It will not be escaped, and date will be executed as an embedded command first.

Note: the backslash is the strongest of all references, so it can even refer to new line characters (< ENTER >).
For example,

linux1@noseeu:~$ echo hi \
> nosee.
hi nosee.
linux1@noseeu:~$ date;\
> cal
Sun 26 Dec 2021 05:28:01 PM UTC
   December 2021      
Su Mo Tu We Th Fr Sa  
          1  2  3  4  
 5  6  7  8  9 10 11  
12 13 14 15 16 17 18  
19 20 21 22 23 24 25  
26 27 28 29 30 31 

Here, the new line character (< ENTER >) loses its special meaning, so at this time, it is not the signal of the end of a line, which means that the subsequent input is followed by the previous line.

5, shell built-in commands

When you enter a command in the shell, the shell parses the command and then decides how to process the command. There are two possibilities. Some commands are inside the shell, which means that the shell can interpret them directly. These commands are internal commands, called built-in commands. All other commands are external commands, that is, independent programs that must run independently.

When a built-in command is entered, the shell runs the command within its own process (no new process is created). When an external command is entered, the shell searches for the appropriate program and then runs the command as a separate process (creating a child process).

A quick way to see if a command is a built-in command is to use type. For example:

linux1@noseeu:~$ type time set date type
time is a shell keyword
set is a shell builtin
date is hashed (/usr/bin/date)
type is a shell builtin

It can be seen that time, set and type are built-in commands of the shell, while date is an external command.

1. View description

Almost all Unix programs are provided with clear pages when they are released, that is, you can use man or info to view their descriptions. However, built-in commands are not a separate program. They are part of the shell. Each shell will provide a large number of commands, so it is unrealistic to develop a separate instruction page for each built-in command.

In fact, all built-in commands are recorded in the instruction page of the shell, that is, you can use man bash to check. However, the specification pages of the shell are very long, and you may need to use search to find the required content (you can use apropos command or man -k command to search).

Another way is to use the help command to view the description of the built-in command, such as help unset

linux1@noseeu:~$ help unset
unset: unset [-f] [-v] [-n] [name ...]
    Unset values and attributes of shell variables and functions.
    ...
    ...
    Exit Status:
    Returns success unless an invalid option is given or a NAME is read-only.

Using the help command without parameters, you can display a summary list of all built-in commands. As shown below:

Use the help -s command to view the syntax of a built-in command:

linux1@noseeu:~$ help -s set
set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]

2. Common built-in commands

alias # Defines an alias for the specified command
echo # Outputs the specified string to STDOUT
exit # Forces the shell to exit with the specified exit status code
export  # Export variables to global variables
fc  # Select a list of commands from history
help # Show help description
history # Display command history
kill # Sends a system signal to the specified process ID(PID)
pwd  # Displays the pathname of the current working directory
set # Set and display the values of environment variables and shell properties
shopt # Turns on / off the value of the variable that controls the optional behavior of the shell
type # Displays how the specified word will be interpreted as a command
unset # Deletes the specified environment variable or shell property

6, Search path

Most commands are not built into the shell, so the shell must find the appropriate program to execute. So where does the shell find external commands?

The shell obtains a series of directory names by looking up the variable PATH, and then looks for the corresponding program under these directories. These directory names are what we call the search PATH.

1. View search path

The search path is a directory list of programs containing all external commands. You can view the search path by using the command echo $PATH.

[21:31 linux1@noseeu ~]$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Search order:
When the shell is looking for an external program, it will check the directories one by one in the specified order in the search path. When it finds the desired external command, it will stop searching and execute the program.

2. Modify search path

The basic idea of modifying the search PATH is to put the command to modify the PATH variable into the initialization file automatically executed at login.

The value of PATH is a string containing several directory names separated by: (colon).

If I have some customized programs in the ~ / bin directory, I can set them as follows:

export PATH="$PATH:$HOME/bin"

Generally, when we modify the PATH value, we add the PATH we want to add to the original PATH.

7, History list

When you enter a command, the shell saves the command to a so-called history list. Then we can access the history list in different ways, call the previous command, modify the command and re-enter the command.

1. View history list

The simplest way to view the history list is to use the < up > and < > down keys, but this method can only view one command at a time.

There is also a more powerful command, which can view all or part of the history command, that is, use the history or fc command.

In the history list, each command is called an event, and each event has an internal number called an event number. The main function of the history list is that it can call commands based on the event number.

View history list:

[23:12 linux1@noseeu ~]$ fc -l
...
931      echo "'now is `date`."
932      help fc
933      fc-l
[23:12 linux1@noseeu ~]$ history
    1  sudo -i
    2  exit
...
934  fc -l
935  history

The number in front of each command is the event number.

2. Call history command

If we want to run command 932 again, we can use fc -s 932 or! 932.

[23:14 linux1@noseeu ~]$ fc -s 932
help fc
fc: fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command]
    Display or execute commands from the history list.
    ...
    Exit Status:
    Returns success or status of executed command; non-zero if an error occurs.
[23:22 linux1@noseeu ~]$ !932
help fc
fc: fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command]
    Display or execute commands from the history list.
    ...
    Exit Status:
    Returns success or status of executed command; non-zero if an error occurs.

If you just want to enter the last command, you can leave the event number: fc -s or!!

3. Retrieve and modify historical commands

The shell allows minor modifications to the command before re executing the historical command. The syntax is as follows:

fc -s pattern=replacement number
!number:s/pattern/replacement

Example:

937      vim sh_test1 
[23:34 linux1@noseeu ~]$ fc -s sh_=Sh 937
vim Shtest1 
[23:35 linux1@noseeu ~]$ !937:s/sh_/Sh
vim Shtest1 

If you just want to modify the last wrong command, you can also do the following:

[23:39 linux1@noseeu ~]$ datw
Command 'datw' not found.
[23:39 linux1@noseeu ~]$ ^w^e
date
Sun 26 Dec 2021 11:40:12 PM UTC
[23:40 linux1@noseeu ~]$ 

Remember that this method is only applicable to the modification of the previous historical command.

4. Search history command

bash also provides a very convenient historical command search method, that is, using < Ctrl > - R (^ R).
If I need to retrieve a command to set PS1 variable, I find it troublesome to check the history list. Then I can press the < Ctrl > - R key and enter the keyword PS:

linux1@noseeu:~$ 
(reverse-i-search)`PS': PS1="\[\033[0;32m\][\A \u\[\033[0;33m\]@\H \w]$ \[\033[0m\]"

If the displayed command is what you want, press enter directly to execute it:

linux1@noseeu:~$ PS1="\[\033[0;32m\][\A \u\[\033[0;33m\]@\H \w]$ \[\033[0m\]"
[23:54 linux1@noseeu ~]$ 

If you don't see the command you want, you can continue to press < Ctrl > - r to search for the next one.

5. Set history list size

The Bash shell stores the history list in a file, so you can continue to use it the next time you log in. To avoid too many history lists, the shell allows you to set the size of the history list by setting a variable.

View shell default settings:

linux1@noseeu:~$ set | grep HIST
HISTCONTROL=ignoreboth
HISTFILE=/home/linux1/.bash_history
HISTFILESIZE=2000
HISTSIZE=1000

If we want to modify the number of history lists displayed by the history command:

linux1@noseeu:~$ export HISTSIZE=10

8, alias

Alias is the name given to a command or a list of commands. You can use an alias as an abbreviation or use an alias to create a custom variant of an existing command.

1. Grammar

Define or display aliases.

alias: alias [-p] [name[=value] ... ]

2. Create alias

We often use ls -al to view the file list. For convenience, we can define aliases as follows:

alias ll='ls -al'

When a command contains spaces or metacharacters, remember to use quotation marks.

linux1@noseeu:~$ alias mytime='date; cal'
linux1@noseeu:~$ mytime
Mon 27 Dec 2021 01:01:56 AM UTC
   December 2021      
Su Mo Tu We Th Fr Sa  
          1  2  3  4  
 5  6  7  8  9 10 11  
12 13 14 15 16 17 18  
19 20 21 22 23 24 25  
26 27 28 29 30 31  

3. View alias

When you need to view the meaning of an alias, you can do this:

linux1@noseeu:~$ alias mytime
alias mytime='date; cal'

Or use the type command:

linux1@noseeu:~$ type mytime
mytime is aliased to `date; cal'

View all aliases:

linux1@noseeu:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -al'
alias ls='ls --color=auto'
alias mytime='date; cal'

4. Remove alias

Syntax: Remove each NAME from the list of defined aliases

unalias [-a] name [name ...]

Example:

linux1@noseeu:~$ unalias mytime 
linux1@noseeu:~$ mytime
mytime: command not found

5. Do not use aliases

Like LS command, many linux will set ls --color=auto in the configuration file by default. At this time, you will use the alias without knowing it. If you don't want to use aliases, you can type a \ 'in front of the command and tell the shell not to use any aliases, such as \ ls.

9, Initialization file

All the settings mentioned above are valid only for the current shell. After logging out and then logging in, those settings no longer exist. If you want to continue to use your settings the next time you log in, you need to add your settings to the initialization file.

Bash shell initialization file includes two, namely login file and environment file.

The initialization file stores all the commands you want to execute automatically each time you log in, while the environment file stores all the programs you want to execute automatically when the new shell starts.

In order to provide more functions, Bash shell also provides a logout file, which is used to store the commands automatically run when logging out.

1. File name

The names of initialization files may be slightly different in different systems, but they are basically the following:

execution environment File name
boot file .bash_profile,.profile,.bash_login
Environmental documents .bashrc
Logout file .bash_logout

The directory where the files are stored is the user directory, that is: / home / user name

Note:
When the system starts, it will be executed first profile and execute bashrc, so if the two files have the same configuration, the former will be overwritten by the latter.

2. Login shell and non login shell

1) Login shell
At any time, such as logging in to the remote host through ssh connection, or logging in to a shell using. This type of shell requires user login authentication to enter. We call it login shell.

The initialization of login shell will execute login file (. profile) and environment file (. bashrc).

2) Non login shell
When login authentication is not required to open a shell window, such as entering bash in a logged in shell window to open a new shell, or simply opening a terminal window in the desktop environment, we call this kind of shell non login shell.

The non login shell executes only the environment files.

3. Initialization file content

The shell initialization file generally contains the following contents:
1) Commands for creating or modifying environment variables
2) Commands to perform all one-time operations
3) Reasonable notes
4) Etc

For example:

# Setting environment variables
HISTSIZE=50
HISTFILESIZE=1000

# Set the file creation mask to control the default permissions of newly created files
umask 077

# Set alias
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Set the default paging program (it will be called when the paging program needs to be called to display data, such as man command)
export PAGER=less
# Set the default option of paging program, which is equivalent to alias less='less -CMs'
export LESS='-CMs'

# View named ` Whether the file of bashrc ` exists. If so, run this file
if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
fi

10, Reference

Bookcase: Chapter 11-14 of UNIX & Linux University Course (US): Harley Hahn, translated by Zhang Jieliang
Blog: Differences between command line interface (CLI), terminal, Shell and TTY
SegmentFault: Linux TTY/PTS overview

Topics: Linux Unix shell