Explanation of environment variables and shell in Linux (Section 3)

Keywords: Linux Operation & Maintenance bash


The content of this section is to explain how to execute the command line of Linux, including basic knowledge such as command interpreter, shell configuration, alias, environment variables, etc.

1.Shell introduction

Shell is the middle bridge between the kernel and users. In fact, it is the interpreter to interpret commands. For example, when the user enters a command on the terminal, the shell explains the function to be realized by the command to the kernel.

Query the current shell (which tool to use as the default command interpreter of the current system)

[root@zaishu ~]# echo $SHELL (which tool to use as the current system default command interpreter)
/bin/bash  //The current command interpreter is bash.

Change shell

The terminal inputs the desired operation shell Name is enough.
[root@zaishu ~]# ksh / / ksh is not supported in the current system
-bash: ksh: command not found
[root@zaishu ~]# SH (use SH as the interpreter for the input command line)

shell command syntax

command -options [argument]
command:The name of the command.
-options:Options. The same command may have many different options to complete different specific functions.
[argument]:Parameter optional
sh-4.2# exit
[root@zaishu ~]# ls (without parameters)
anaconda-ks.cfg  ip.sh  lamp-master  lamp-master.zip  sdcms_php_ee_2.4.2.zip  web
[root@zaishu ~]# ls -a (with parameters)
.   anaconda-ks.cfg  .bash_logout   .bashrc  ip.sh        lamp-master.zip  .pki                    .tcshrc
..  .bash_history    .bash_profile  .cshrc   lamp-master  .mysql_history   sdcms_php_ee_2.4.2.zip  web

2. Common special symbols of shell (wildcard)


It can represent any character (including null character) or a string composed of multiple characters. for example

//View the information of all files and folders starting with a in the current directory
[root@zaishu ~]# ls a*
aa  aaa  aabbc  anaconda-ks.cfg


Represents a single character.

[root@zaishu ~]# ls a?
aa   //View the information of all files and folders with length 2 starting with a in the current directory

(3)"[ ]"

Specified range

For example, create five new files named a,b,c,d,e. 
[root@zaishu ~]# touch a 
[root@zaishu ~]# touch b
[root@zaishu ~]# touch c
[root@zaishu ~]# touch d
[root@zaishu ~]# touch e
[root@zaishu ~]# ls [a-d]
a  b  c  d  //View only files named a~d


Exclude symbols, which need to be used in conjunction with the "[]" symbol.

[root@zaishu ~]# ls [!a-d] / / the file name is a single character, and the files of a-d are excluded
1  e


A split symbol used to separate commands when multiple commands are entered on one line.

//First create a file zs, and then view the file information in the current directory
[root@zaishu ~]# touch zs ; ls 
1  a  aa  aaa  aabbc  anaconda-ks.cfg  b  c  d  e  ip.sh  lamp-master  lamp-master.zip  sdcms_php_ee_2.4.2.zip  web  zs


Command substitution character, containing the string of the command to be executed.

[root@zaishu ~]# echo `ls' / / displays the result of the command ls
1 a aa aaa aabbc anaconda-ks.cfg b c d e ip.sh lamp-master lamp-master.zip sdcms_php_ee_2.4.2.zip web zs
[root@zaishu ~]# echo 'ls' / / LS here is just an ordinary string

Comment symbol, # the first line is a comment and will not be executed.

[root@zaishu ~]# # touch ceshi  //With # this line, it will not be executed. It is often used as a comment when writing shell scripts
[root@zaishu ~]# ls ceshi 
ls: cannot access ceshi: No such file or directory

3. Command or file automatic completion (tab)

In linux, shell provides the function of command line automatic completion. When entering a file name, just enter the first few characters of the file name, and then press Tab. Shell can automatically complete the file name.
1. When there is only one file starting with fs in the directory:

[root@zaishu etc]# cd /etc/
[root@zaishu etc]# Cat fs < --- there is only one file starting with fs in the current directory
 When pressed Tab Key, Shell Automatically"fs"It's complete"fatab",This is because the current/etc Only in the directory fstab So"fs"At the beginning, so Shell You can confirm that the file name you want to enter here is fstab. 

2. If the current directory contains multiple directories or files starting with the specified character (or string), Shell will give all files or directories starting with the specified character or string in the form of a list for users to select. Execute the following command:

[root@zaishu ~]# ls a
a                aa               aaa              aabbc            anaconda-ks.cfg  
Press Tab Key, Shell The list will be displayed to all users in the current directory"a"Start with a file or directory.

The command line completion function provided by Shell is not only applicable to complete file names, but also applicable to all Linux commands.

3. When you enter ca and press the Tab key twice in a row, the Shell will list all Linux commands starting with "ca", as follows:

[root@zaishu ~]# ca
cacertdir_rehash  ca-legacy         capsh             case              catchsegv         
cal               caller            captoinfo         cat               catman

4. Alias

1. Set alias
Alias alias = 'original command - options / parameters'

[root@zaishu ~]# alias lt='ls -lt'
This is set ls -lt The alias for the command is lt,Input at terminal lt When, it is equivalent to entering ls -lt command
[root@zaishu ~]# lt
total 4580
-rw-r--r--  1 root root       0 Oct 22 11:26 zs
-rw-r--r--  1 root root       0 Oct 22 11:25 1
-rw-r--r--  1 root root       0 Oct 22 11:22 e
-rw-r--r--  1 root root       0 Oct 22 11:22 d
-rw-r--r--  1 root root       0 Oct 22 11:22 c
-rw-r--r--  1 root root       0 Oct 22 11:21 b
-rw-r--r--  1 root root       0 Oct 22 11:21 a
-rw-r--r--  1 root root       0 Oct 22 11:14 aabbc
-rw-r--r--  1 root root       0 Oct 22 11:14 aaa
-rw-r--r--  1 root root       0 Oct 22 11:14 aa
-rwxr-xr-x  1 root root       0 Oct 21 15:31 ip.sh
drwxr-xr-x. 2 root root       6 Oct 14 17:52 web
drwxr-xr-x. 6 root root     194 Oct 14 17:22 lamp-master
-rw-r--r--. 1 root root 4459288 Oct 14 16:47 sdcms_php_ee_2.4.2.zip
-rw-r--r--. 1 root root  224957 Oct 14 15:41 lamp-master.zip
-rw-------. 1 root root    1368 Oct  8  2020 anaconda-ks.cfg

Note: when defining an alias, there must be no spaces around the equal sign, otherwise the shell cannot decide what you need to do. Quotation marks are required only if the command contains spaces or special characters.

2. View the alias list that has been set
If you type the alias command without any parameters, all defined aliases are displayed.

[root@zaishu ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias lt='ls -lt'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

3. Delete alias

unalias Alias 1
[root@zaishu ~]# unalias lt
[root@zaishu ~]# lt
-bash: lt: command not found

4. Set the alias available for each login
The alias command only applies to the current login operation. If you want to use the aliases of these commands every time you log in, you can store the corresponding alias commands in ~ /. bashrc or. Bash_ In the profile file.

open~/.bashrc File, enter the file you want to set alias Command, save, and then run
[root@zaishu ~]# vi .bashrc 


#User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias lt='ls -lt'

#Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc

Make it effective immediately

source ~/.bashrc1

This allows you to use the set command alias after each login.

5. Command execution process

In short, the execution process of Linux commands is divided into the following four steps.

  1. Judgment path
    Judge whether the user enters a command in the form of absolute path or relative path (such as / bin/ls). If so, execute it directly.

  2. Check alias
    The Linux system checks whether the command entered by the user is an "alias command". You know, you can customize the alias of the existing command through the alias command, that is, replace the original command name with a customized command name.
    For example, the frequently used rm command is actually the overall alias rm -i:

[root@localhost ~]# alias rm
alias rm='rm -i'
This makes when using rm When the command deletes the specified file, Linux The system will ask us to confirm whether to delete again. For example:
[root@localhost ~]# RM a.txt < -- it is assumed that a.txt file already exists in the current directory
rm: remove regular file 'a.txt'? y  <-- Manual input y,OK to delete
[root@localhost ~]#
You can use it here unalias Command, will Linux System set rm Delete the alias and execute the following command:
[root@localhost ~]# alias rm
alias rm='rm -i'
[root@localhost ~]# unalias rm
[root@localhost ~]# rm a.txt
[root@localhost ~]#  < -- delete directly without asking
 Note that this is for demonstration purposes only unalisa Usage of. Use caution when deleting without prompt in actual work
  1. Determine whether it is an internal command or an external command
    The Linux command line interpreter determines whether the command entered by the user is an internal command or an external command. Internal commands refer to the commands inside the interpreter, which will be executed directly; The commands that users usually input are external commands. These commands specify the location through Path in the environment variable.
    The internal commands are provided by the Shell and can be read directly from the memory as the system starts; The external command only has the corresponding executable file in the system, which needs to be read during execution.
To determine whether a command belongs to an internal command or an external command, you can use type Command implementation. For example:
[root@localhost ~]# type pwd
pwd is a shell builtin  <-- pwd It's an internal command
[root@localhost ~]# type top
top is /usr/bin/top  <-- top Is an external command
  1. Find the executable corresponding to the external command
    When the user executes an external command, the system will find the executable files of the command in the specified multiple paths, and the variables defining these paths are called PATH environment variables. Their function is to tell the Shell where the executable files are stored, that is, the Shell will find them one by one in the multiple paths contained in the PATH variable until they are found (if it cannot be found, the Shell will provide the user "this command cannot be found").
[root@zaishu ~]# echo $PATH

6. Environmental variables and effects

Variable is the data type used by the computer system to save variable values. The corresponding variable values can be extracted directly through the variable name. In Linux system, environment variables are used to define some parameters of the system running environment, such as different HOME directory (HOME) and MAIL storage location (MAIL) of each user.
The names of environment variables in Linux systems are generally capitalized. You can use the env command to view all environment variables in the Linux system. Execute the command as follows:

[root@localhost ~]# env

The Linux system can run normally and provide services to users. Hundreds of environment variables are needed to work together. Here are 10 very important environment variables, as shown in Table 1.

Table 1 10 important environment variables in Linux system

As a multi-user and multi task operating system, Linux has different values for the same environment variable because of different user identities.
Use the following command to view the values of the HOME variable under different user identities:

[root@zaishu ~]# echo $HOME
[root@zaishu ~]# The su - oracle //su command is used to switch users. See the following for specific usage

[oracle@zaishu ~]$ echo $HOME

7. Custom variables

Environment variables are composed of fixed variable names and variable values set by users or the system. You can create environment variables to meet your work needs. For example, set the environment variable named web and execute the following command:

[root@zaishu ~]# mkdir -p /www/zaishu
[root@zaishu ~]# export web=/www/zaishu
[root@zaishu ~]# echo $web
[root@zaishu ~]# cd $web
Such environment variables are not global and have limited scope. They cannot be used by other users by default:

[root@zaishu zaishu]# su - oracle
[oracle@zaishu ~]$ cd $web
[oracle@zaishu ~]$ pwd
[oracle@zaishu ~]$ echo $web

8. PATH environment variables and functions

Before explaining the PATH environment variable, first introduce the which command, which is used to find the absolute PATH of a command. For example:

[oracle@zaishu ~]$ which rm
[oracle@zaishu ~]$ which rmdir
[oracle@zaishu ~]$ which ls//It uses the alias command as an alias, and ls actually executes ls --color=auto
alias ls='ls --color=auto'

By using the which command, you can find the absolute PATH where the command is located. When using rm, rmdir, ls and other commands before, you can use them directly regardless of the current directory, without indicating the location of the command execution file. Because you can find the command through the directory corresponding to the PATH environment variable.

First, execute the following command:

[root@zaishu zaishu]# echo $PATH

The echo command here is used to output the value of PATH environment variable. The content of PATH environment variable is composed of directories separated by colon ":. When executing a command, Linux will search the executable file of the command according to the directory contained in the PATH. Once found, it will be executed normally; Otherwise, you will be prompted that the command cannot be found.
If multiple directories in the directory included in PATH contain the executable file of a command, the executable file searched first will be executed.
/The bin directory is already contained in the PATH environment variable, so Linux can find this command when using commands such as rm, rmdir, ls, etc.

As a counter example, move the ls command to the / root directory. Because the PATH environment variable does not contain this directory, Linux will not be able to find the executable file of the command and prompt No such file or directory when executing directly with the ls command name:

[root@zaishu zaishu]# mv /bin/ls ~/
[root@zaishu zaishu]# ls
-bash: /usr/bin/ls: No such file or directory

At this time, you still want to use the ls command. There are two methods. One is to directly add / root to the PATH environment variable, for example:

[root@zaishu zaishu]# PATH=$PATH:/root
[root@zaishu zaishu]# ls
[root@zaishu zaishu]# 

Another way is to use this command as an absolute path, for example:

[root@zaishu zaishu]# /root/ls
[root@zaishu zaishu]# 


The content of this chapter is simple but also very important

Posted by ReKoNiZe on Thu, 21 Oct 2021 23:40:27 -0700