Shell core knowledge notes for beginners (Part 1)

Table of contents

1. Shell overview

1. The Shell parser provided by Linux has

2. The relationship between bash and sh

3. The default parser of Centos is bash

2. Getting Started with Shell Script 

1. Script format

2. The first shell script: helloworld.sh

3. Common execution methods of scripts

3. Variables

1. System predefined variables

2. Custom variables

3. Special variables

Fourth, the operator


1. Shell overview

        Shell is a command interpreter that receives application/user commands and then invokes the operating system kernel. Shell is also a very powerful programming language, easy to write, easy to debug, and flexible.

1. The Shell parser provided by Linux has

$ cat /etc/shells

/bin/sh

/bin/bash

/usr/bin/sh

/usr/bin/bash

/bin/tcsh

/bin/csh

2. The relationship between bash and sh

sh -> bash

3. The default parser of Centos is bash

$ echo $SHELL

/bin/bash 

2. Getting Started with Shell Script 

1. Script format

Script starts with #!/bin/bash (specify parser)

2. The first shell script: helloworld.sh

Create a shell script that outputs helloworld

enter

$ touch helloworld.sh

$ vim helloworld.sh

Press i to enter edit mode 

Enter the following in the edit box

#!/bin/bash

echo "helloworld"

(Press esc to exit the editing mode, enter: wq to save and exit, the subsequent editing operations are similar, no explanation will be given)

3. Common execution methods of scripts

The first one: use the relative path or absolute path of the bash or sh+ script (do not give the script +x permission)

like:

$ sh ./helloworld.sh relative path of sh+ script  

$ sh /home/my/shell/helloworld.sh The absolute path of the sh+ script

$ bash ./helloworld.sh relative path of bash+ script

$ bash /home/my/shell/helloworld bash+ script absolute path

The second type: Execute the script using the absolute path or relative path of the input script (must have executable permission +x)

1. Give the helloworld script the execution permission (the actual use will be slightly different, here is only for learning)

$ chmod  +x  helloworld.sh

At this time, enter ls and find that the file has changed color, indicating success (Note: the tab key has an upgrade function)

2. Execute the script

$ ./helloworld.sh relative path

$ /home/my/shell/helloworld absolute path

***** The essence of the first execution method is that the bash parser will help you execute the script, so the script itself does not need execution permission. The second execution method is essentially that the script needs to be executed by itself, so it needs execution permission.

The third way: add "." or source before the path of the script

$ .  helloworld.sh

$ source  helloworld.sh

***** The first two methods open a subshell in the current shell to execute the script content. When the script content ends, the subshell closes and returns to the parent shell.

        The third way is to add "." or source before the script path, so that the script content can be executed in the current shell, and there is no need to open a subshell, which is why the /etc/profile file needs to be sourced after modification s reason.

        The difference between opening a subshell and not opening a subshell is the inheritance relationship of environment variables. The modification in the subshell is not visible to the parent shell. That is to say, if the data is modified in the subshell, it will be restored after exiting.

3. Variables

1. System predefined variables

Common System Variables

$HOME / $PWD / $SHELL / $USER

1. View the value of the system variable

$ echo  $HOME

2. Display all variables in the current shell: set

2. Custom variables

1. Basic grammar

Define variables: variable name = variable value, note that there must be no spaces before and after the = sign

Undo variable: unset variable

Declare static variables: readonly variables, note: cannot be nuset

2. Variable definition rules

Variable names can be composed of letters, numbers, and underscores, but cannot start with a number. It is recommended that the environment variable name be capitalized.

There must be no spaces on either side of the equal sign.

In bash, the default types of variables are all string types, which cannot be directly used for numerical operations.

If the value of the variable has spaces, it needs to be enclosed in double quotes or single quotes.

***** $ export variable name can promote the variable to a global environment variable, which can be used by other shell programs

3. Special variables

1. $n

n is a number, $0 represents the name of the script, $1~$9 represent the first to ninth parameters, and more than ten parameters need to be enclosed in braces, such as ${10}

example:

$ touch  parameter.sh

$ vim  parameter.sh

press i to enter edit

#!/bin/bash

echo $0

echo $1

Esc plus: wq to save and exit

call

$ chmod +x parameter.sh

$ ./parameter.sh aaa bbb

2.$*、$@

$*: This variable represents all the parameters in the command line, $* regards all parameters as a whole

$@: This variable also represents all the parameters in the command line, but $@ treats each parameter differently

***** Add quotation marks when using, otherwise it will have no effect

3.$?

The return status of the last executed command, if the value of this variable is 0, it proves that the last command was executed correctly; if the value of this variable is non-zero (the specific number is determined by the command itself), it proves that the last command was executed Not correct anymore.

Determine whether the helloworld.sh script is executed correctly

$ ./helloworld.sh

$ echo $?

Fourth, the operator

Basic syntax: "$((operation expression))" or "$[operator]"

# s=$[ (2+3)*4 ]

# echo $s

Guess you like

Origin blog.csdn.net/a2285786446/article/details/127743967