shell 02 (shell variable)

1. Shell variables

Variables are used to store and manage temporary data, which are all in running memory.

1.1 Variable type

1.1.1 System environment variables

  • It is a shared variable provided by the system.
  • The variables defined in the configuration file loaded by the Linux system are shared by all Shell programs.

Shell configuration file classification

  • Global configuration file

/etc/profile
/etc/profile.d/*sh

/etc/bashrc

  • personal profile

Current user/.bash_profile

currentuser/.bashrc


Under normal circumstances, we operate directly on the global configuration.

1.1.1.1 Classification of environment variables

In Linux systems, environment variables can be roughly divided into system-level environment variables and user-level environment variables according to their different scopes .

  • System-level environment variables: The variables in the Shell environment load global configuration file are shared by all users and all Shel programs, and are shared globally.
  • User-level environment variables: The shell environment loads the variables in the personal configuration file and shares them with the current user's shell program and is used by the logged-in user.

1.1.1.2 View system environment variables

env    

set //View system environment variables + custom variables + functions 

1.1.1.3 Commonly used system environment variables

echo $PATH


1.1.2 Custom variables

1.1.2.1 Custom local variables

They are variables defined in a script file. Variables that can only be used in this script file are local variables.

Syntax: var_name=value

  • 1. Variable names can consist of letters, numbers and underscores, but cannot start with numbers.
  • 2. There cannot be spaces on both sides of the equal sign.
  • 3. In the bash environment, the default types of variables are string types, and numerical operations cannot be performed directly.
  • 4. If the value of the variable has spaces, it must be enclosed in double quotes.
  • 5. You cannot use Shell keywords as variable names

Query variable value syntax:

echo $var_name //Use the variable name directly

echo ${var_name} //curly brackets

Curly braces are suitable for splicing strings

 

Add, delete, modify and check

Delete variable syntax:

unset var_name 


1.1.2.2 Custom constants

A variable that cannot be modified after its value is set is called a constant, also called a read-only variable. 

Syntax: readonly var_name 


1.1.2.3 Custom global variables

It is to define a global variable in the current script file. This global variable can be used in the current Shell environment and sub-Shell environment.

Introduction to parent-child shellI environment
For example: There are two shell script files A.sh and B.sh.
If the B.sh script file is executed in the A.sh script file, then Ash is the parent shell environment and B.sh is the child shell environment.

Syntax: export var_name1 var_name2


1.1.3 Special symbol variables

1.1.3.1 $n

  • $n is used to receive the parameters passed in when the script file is executed.
  • $1~$9, represents obtaining the first input parameter to the 9th input parameter
  • The format for obtaining parameters above the 10th parameter: ${number} , otherwise it cannot be obtained
  • $0 Get the name of the current shell script file

Execute script file and pass in parameter syntax:

sh script file Pass in parameter 1 Pass in parameter 2...

#!/bin/bash

#命令1: 打印当前脚本文件名宁
echo "当前脚本文件名称:$O"

命令2: 打印第1个输入参数
echo "第一个输入参数:$1"

命令3: 打印第2个输入参数
echo "第二个输入参数:$2"

命令4:打印第10个输入参数
echo "第十个输入参数不带花括号获取:$10"
echo "第十个输入参数带花括号获取:${10}"

1.1.3.2 $#

  • $# Get the number of all input parameters

1.1.3.3 $*  $@

$* $@ is to get all input parameters and use them to output all parameters in the future.

  • Without double quotes, the function is the same     

 $* $@ is to get all input parameters, the format is: $1 $2 ... $n 

  • Use double quotes, then 

"$*" gets all parameters concatenated into a string, the format is: "$1 $2 ... $n"

"$@" obtains a set of parameter list objects in the format: "$1" $2" ... "$n"

 

Loop syntax in shell:

for var in 列表变量
do   #循环开始
   命令  #循环体

done #循环结束

Copy the command in the vim editing interface, use esc to exit the editing mode, and enter 5yy to copy the line where the cursor is and a total of 5 lines below it.

Press p to output

 

Code:

echo "参数一:$0"

echo "参数二:$1"

echo "参数三:$2"

echo "参数十一:$11"
echo "参数十一:${11}"

echo "输入所有参数的总数:$#"
echo '使用$*:'$*
echo '使用${*}:'${*}

echo '使用$@: '$@
echo '使用${@}:'${@}

echo '循环遍历$*所有参数:'
for num in "$*"
do
        echo $num
done

echo '循环遍历$@所有参数:'
for num in "$@"
do
        echo $num
done

Execute sh test_darren.sh 1 2 3 4 5 6 7 8 9 10 11 output:

(base) w@w-System-Product-Name:~$ sh test_darren.sh 1 2 3 4 5 6 7 8  9 10 11
参数一:test_darren.sh
参数二:1
参数三:2
参数十一:11
参数十一:11
输入所有参数的总数:11
使用$*:1 2 3 4 5 6 7 8 9 10 11
使用${*}:1 2 3 4 5 6 7 8 9 10 11
使用$@: 1 2 3 4 5 6 7 8 9 10 11
使用${@}:1 2 3 4 5 6 7 8 9 10 11
循环遍历$*所有参数:
1 2 3 4 5 6 7 8 9 10 11
循环遍历$@所有参数:
1
2
3
4
5
6
7
8
9
10
11

It can be seen that when looping through,

"$*" direct output

"$@" output one by one

1.1.3.4 $?

Used to obtain the exit status code of the previous shell command , or the return value of the function

  • The execution of each shell command has a return value. This return value is used to indicate whether the command execution was successful.
  • Generally speaking, returning 0 means the command execution was successful, and non-0 means the execution failed.

1.1.3.5 $$

Used to obtain the process ID number of the current ShelI environment

Check the current shell environment process number

ps -aux|grep bash 

Notice:

echo "Value is $VAR"    #Output "Value is" followed by the value of the variable

echo ' Value is $VAR' #Output literal string "Value is $VAR"

  • Within double quotes, you can perform variable substitution, that is, insert the value of the variable into the string. For example, if $VARis a variable, echo "Value is $VAR""Value is" will be output followed by the value of the variable.
  • Within single quotes, variables are not replaced by their values. echo 'Value is $VAR'Will output the literal string "Value is $VAR".

Guess you like

Origin blog.csdn.net/peng_258/article/details/132426552