Linux: Introduction to Shell, BASH and Shell Script

Shell

What is Shell

Shell (shell program) is the user interface of the system, which provides an interface for the user to interact with the kernel. It receives the command entered by the user and sends it to the kernel for execution.

Insert picture description here

In fact, Shell is a command interpreter, which interprets the commands entered by the user and sends them to the kernel. Not only that, Shell has its own programming language for editing commands, which allows users to write programs composed of shell commands. Shell programming language has many characteristics of ordinary programming languages, for example, it also has loop structure and branch control structure, etc. Shell programs written in this programming language have the same effect as other applications

As long as the software that can operate the application program can be called a shell program, the narrow shell program refers to the command line software , such as the BASH described later in this article; the broad shell program includes the software of the graphical user interface mode, because the graphics The user interface can also operate various applications to call the kernel work

BASH

What is BASH

Shell has many different versions according to its development, BASH is one of them, BASH (Bourne Again Shell) is the version used by Linux by default

Shell variable function

variable

Variables are data types used by computer systems to store variable values. We can directly extract the corresponding variable value through the variable name. It is to replace some settings or a string of reserved data with a set of words or symbols.
Insert picture description here

Environment variable

In Linux systems, environment variables are used to define some parameters of the operating environment of the system, such as the different home directories (HOME) and mail storage location (MAIL) of each user. In order to distinguish from custom variables, environment variables are usually expressed in uppercase characters

As a multi-user and multi-task operating system, Linux can provide each user with an independent and suitable working environment. Therefore, a same environment variable will have different values ​​due to different user identities. As shown below:
Insert picture description here

Use and setting of variables: echo, =, unset

echo : You can use the echo command to use variables. When variables are used, they must be preceded by a dollar sign $. For example, to view the contents of the PATH variable:

echo $PATH

Insert picture description here
= : Use = to set or modify the content of a variable. For example, to set the content of the myname variable to WangGarrison, then:

myname=WangGarrison

Insert picture description here
When setting variables, they must meet certain regulations, otherwise the setting will fail. The rules are as follows:

  1. Variable and variable content are connected with an =
  2. No spaces are allowed on both sides of the equal sign
  3. Variable names can only be English letters and numbers, and cannot start with numbers
  4. There are spaces in the variable content, you can use single or double quotes to enclose the variable

unset : unset can cancel the variable, for example, cancel the setting of myname:

unset myname

Insert picture description here

Function of environment variables

Environment variables can help us achieve many functions, including the conversion of the root directory (home folder), the display of prompt characters, the path to perform file search, etc.

You can use the env and export commands to view the default environment variables of the current shell environment. env is short for environment

env

Insert picture description here
The 10 important environment variables of Linux are as follows:

Environment variable name effect
HOME User's home directory
SHELL Shell interpreter name used by the user
PATH Path to perform file search
EDITOR User default text interpreter
RANDOM Random number variable
LANG Language data
HISTSIZE Number of historical command records output
HISTFILESIZE Number of saved historical command records
PS1 Prompt of Bash interpreter
MAIL Path to save emails

Use the set command to view all variables (including environment variables and custom variables)

set

Convert custom variables to environment variables

The main difference between a custom variable and an environment variable is that the environment variable will continue to be referenced by the child process, while the custom variable will not. The correlation between the parent process and the child process is as follows: the
Insert picture description here
child process only inherits the environment variables of the parent process, and the child process does not Will inherit the custom variables of the parent process. Therefore, the original bash custom variables cannot be used in the child process after entering the child process, and can not be used until the child process exits.

So if you want the custom variables of the parent process to be used in the child process, you need to convert the custom variables into environment variables and use the export command to convert

export 变量名称

Specific introduction of export command

Variable keyboard reading, array and declaration

read : used to read variables input from the keyboard, which can be used to interact with the user

read [-pt] variable  #-p后面可以接提示字符   -t后面可以接等待的秒数

Insert picture description here
declare, typeset : declare or typeset is the same function, which is to declare the type of the variable

declare [-aixr] variable
-a:将后面名为variable的变量定义成为数组(array)类型
-i:将后面名为variable的变量定义成为整数(integer)类型
-x:用法与export一样,就是将后面的variable变成环境变量
-r:将变量设置成为readonly类型,该变量不可被更改内容,也不能unset

By default, the variable type defaults to string, as shown in the figure below, variable a defaults to string 2+3+4
Insert picture description here
array variable type

var[index]=content  #数组名为var,下标是index,内容是content,

E.g:
Insert picture description here

Command aliases and historical commands

Command alias setting : alias, unalias

alias lm='ls -al | more'  #把ls -al |more命令简化为lm

You can directly type alias to see which command aliases are currently available. As shown below:
Insert picture description here
delete command alias

unalias lm

History command

history [n]  
history [-c]  
history [-raw]
选项与参数:
n:列出最近的n条命令行表
-c:将目前的shell中的所有history内容全部清除
-a:将目前新增的history命令新增入histfiles中,若没有histfiles,则默认写入~/.bash_history
-r:将histfiles的内容读到目前这个shell的history中
-w:将目前的history记录内容写到histfiles中

When we log in to the Linux host with bash, the system will actively read the previously executed commands from the home directory ~/.bash_history. The number of data recorded in this directory is related to the environment variable HISTFILESIZE of bash.

Shell Script

What is ShellScript

Shell Script is a shell script, which is a script written for the shell. We write some of the grammar and commands specified by the shell, together with functions such as regular expressions, pipeline commands, and data stream redirection, into a plain text file to achieve the processing purpose we want, and then add the ".sh" extension. , This is "Shell script"

In short, we can successfully communicate with the computer only by interpreting our commands and other requests through the Shell tool. At the same time, with Shell script, a "program" that can process commands in batches, we can communicate better with the computer.

Reference books

"Niaoge's Linux Private Kitchen" Fourth Edition Chapter 10 Understanding and Learning BASH

Guess you like

Origin blog.csdn.net/huifaguangdemao/article/details/108393856