Variables and operators in Shell

table of Contents

System variables

1. Commonly used system variables

2. Example operation

View the values ​​of system variables

Custom variable

1. Basic grammar

2. Variable definition rules

3. Example operation

Special variable

Special variable: $ n

Special variable: $ #

Special variables: $ *, $ @

Special variable: $?

Operator



System variables

1. Commonly used system variables

$HOME,$PWD,$SHELL,$USER等

2. Example operation

  • View the values ​​of system variables

[es@master ~]$ echo $HOME
/home/es
  • Display all variables in the current Shell: set

Custom variable

1. Basic grammar

 (1) Define variable: variable = value

(2) Undo variable: unset variable

(3) Declare static variables: readonly variables, note: cannot be unset

2. Variable definition rules

(1) The variable name can be composed of letters, numbers, and underscores, but it cannot start with a number . It is recommended to capitalize the environment variable name .

( 2 ) No spaces on both sides of the equal sign

(3) In bash, the default types of variables are all string types, and numerical operations cannot be performed directly.

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

3. Example operation

 (1) Define variable A

(2) Reassign the variable A

(3) Undo variable A

(4) Declare static variable B = 2, cannot be unset

(5) In bash, the default types of variables are all string types, and numerical operations cannot be performed directly

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

(7) Variables can be promoted to global environment variables, which can be used by other Shell programs

Note: The variables defined in the shell (and shell scripts) only take effect in the current shell. If you need to make the variable still visible in its child process, you need to use the export command to define the variable, but it ca n’t affect the parent process anyway. Environment variables

As shown in the figure, the variables defined in the parent shell on the left have an effect in the child shell.

Special variable

Special variable: $ n

1. Basic grammar

       $ n (Function description: n is a number, $ 0 represents the script name, $ 1- $ 9 represents the first to ninth parameters, more than ten parameters, more than ten parameters need to be included in braces, such as $ {10})

2. Case study

(1) Output the script file name, input parameter 1 and input parameter 2 values

#!/bin/bash
echo $0 $1 $2

Special variable: $ #

1. Basic grammar

       $ # (Function description: Get the number of all input parameters, commonly used in loop).

2. Case study

(1) Get the number of input parameters

#!/bin/bash
echo $0 $1 $2
echo $#

Special variables: $ *, $ @

1. Basic grammar

       $ * (Function description: This variable represents all parameters on the command line, $ * treats all parameters as a whole)

       $ @ (Function description: This variable also represents all the parameters on the command line, but $ @ treats each parameter differently)

2. Case study

(1) Print all input parameters

#!/bin/bash
echo $0 $1 $2
echo $#
echo $*
echo $@

Special variable: $?

1. Basic grammar

$? (Function description: The return status of the last command executed. 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 (which number is determined by the command itself) Prove that the last command was executed incorrectly.)

2. Case study

       (1) Determine whether the helloworld.sh script is executed correctly

Operator

1. Basic grammar

(1) "$ ((expression))" or "$ [expression]"

(2) expr   +,-, \ *, /,%     addition, subtraction, multiplication, division, remainder

Note: There must be spaces between expr operators

2. Case practice:

(1) Calculate the value of 3 + 2

 

(2) Calculate the value of 3-2

 

(3) Calculate the value of (2 + 3) X4

       (A) Expr completes calculation in one step

      (B) Using the $ [calculation formula] method

Note: Try to take the simplest way, that is, use the method of [calculation].

 

 

 

 

 

 

 

Published 39 original articles · won praise 1 · views 4620

Guess you like

Origin blog.csdn.net/thetimelyrain/article/details/104252417