Linux learning (8) - Shell Variables

table of Contents

Outline

Bash Variables What is a variable and variable classification

1, the variable naming rules

2, store data in accordance with classification variables

3, for categorical variables

Users Bash Variables Custom Variables

1, the definition of variables

2, variable call

3, variable superimposed

4, variable view

5, delete variables

Environment Variables Bash Variables

1, set the environment variable

2, view the environment variables

3, delete the environment variable

4, commonly used environment variables

5, PATH environment variable

6, PS1 environment variable

Language variables Bash Variables

1, the current query language

2, language variables LANG

3, query system default language

4, Linux Chinese support

Location parameter variables Bash Variables

1, the position of the variable parameter

Predefined variables Bash Variables

1, predefined variables

2, to receive keyboard input


 

Video tutorial lesson but learning Mu: https://www.imooc.com/learn/336

This section summarizes the study and made notes.

 

Outline

shell comparison with other languages:

  • Realize the functions of PHP and Java

  • Shell simplify management operations

 

Bash Variables What is a variable and variable classification

Variable: a temporary value assigned to a variable, such as X = 2, name = "John Doe", age = 45, can be variable by name, age acquires its value, and the value can be modified.

1, the variable naming rules

  • Variable names must begin with a letter or an underscore, the middle can only consist of letters, numbers and underscores.

  • Variable name can not exceed 255 characters.

  • Variable names must be unique within the effective range, ie within the same range can not have two variables of the same name.

  • In Bash, the default type of a variable is a string type.

  • In any system, directory names, file names, variable names must be meaningful, such as name, age rather than abc, xyz its meaning is unknown.

2, store data in accordance with classification variables

  • String, such as info = "hello world"

  • Integer, such num = 12

  • Float, is a decimal, such as num = 12.5

  • Date type

However, the default type is string in Bash.

3, for categorical variables

  • User-defined variables. Variables are user-defined.

  • Environment variables. This variable is stored in the main operating system and environment-related data. Variables can be customized, but the environment variable names and the role of the system in force is fixed.

  • Location parameter variables. In fact, one of the predefined variables, this variable is mainly used to pass parameters to the script or data among the variable names can not be customized, variable effect is fixed.

  • Predefined variables. Bash is already defined variable, the variable name can not be customized, variable effect is fixed.

 

Users Bash Variables Custom Variables

  • It can not be used as the beginning of the digital variable name

  • No spaces variable assignment

  • Variable value required for the space, there are single or double quotes

1, the definition of variables

变量名=变量值
	例如:
		x=34
		name="张三"

2, variable call

echo $变量名
	例如:
		echo $x
		echo $name

Note that the output value of x is a string actually, you can not participate in operation. Such as $ x + $ y error.

Example 1: $ x + $ y command

3, variable superimposed

x=123
x="$x"456
	输出:123456
x=123
x=${x}789
	输出:123789

4, variable view

set
	选项:
		-u 如果设定此选项,调用未声明变量时会报错(默认无任何提示)

able to query the system to set all the variables that are running, in addition to the default existing environment variables as well as variables define yourself.

Example 1: set command to see all the variables

Example 2: set -u command , variable does not exist error.

5, delete variables

unset 变量名
	删除变量

Example 1: unset x command to delete variables

 

Bash Variables Environment Variables

Execute bash command to enter the current sub-Shell Shell, with exit to return to the parent command from the sub Shell Shell, you can pstree view commands.

Environment variables and user-defined variables difference:

  • Environment variables are global variables, you can also call in the sub-Shell

  • User-defined variables are local

Environment variable name and role of the system in force is fixed.

1, set the environment variable

export 变量名=变量值
或
变量名=变量值
export 变量名

Example 1: export e = 'environment variable' command , set the environment variable.

Use the set command to view or env environment variable settings.

2, view the environment variables

set
	查看所有变量
env 
	查看环境变量

Example 1: env command , view the environment variables within the system.

Example 2: bash command , because of environmental variables are global variables, it can also be seen in the sub-bash.

Set two variables x and y, where the former is a common variable, which is the environment variables, command set can be viewed.

All values ​​of the variables can be successfully exported.

Use bash command to enter the sub Shell, use the set command again to view or output values ​​of two variables.

The former can see no value.

3, delete the environment variable

unset 变量名
	删除环境变量

4, commonly used environment variables

  • HOSTNAME: hostname

  • SHELL: current shell

  • TERM: terminal environment

  • HISTSIZE: Command History

  • SSH_CLIENT: ssh current operating environment is connected, the recording where the client IP

  • SSH_TTY: When connected to the terminal pts ssh / 1

  • USER: user currently logged on

Note: We recommend the environment variable capital letters to distinguish between ordinary variables.

1 case: $ SHELL echo command , the output value of the environment variable SHELL.

5, PATH environment variable

Meaning that the ls command systems, does not require typing the command absolute path will be able to perform, because to look the PATH path.

You can view the path through the PATH echo $ PATH command.

If you want permanent environment variable, it needs to be written to the file.

PATH变量:系统查找命令的路径
echo $PATH
	查看PATH环境变量
PATH="$PATH":/root/sh
	增加PATH变量的值

Example 1: echo $ PATH command to view the PATH, wherein each path separated by a colon.

Example 2: PATH = "$ PATH" : / root / sh commands , adding the PATH, where "$ PATH" refers to the value of the PATH variable ":" refers to a flag used to separate the PATH, "/ root / sh "refers to the path to be added to the pATH.

6, PS1 environment variable

echo $PS1
	查看PS1环境变量的值
PS1变量
	命令提示符设置。
	\d:显示日期,格式为“星期 月 日”
	\H:显示完整的主机名,如默认主机名“localhost.localdomain”
	\t:显示24小时制时间,格式为“HH:MM:SS”
	\A:显示24小时制时间,格式为“HH:MM”
	\u:显示当前用户名
	\w:显示当前所在目录的完整名称
	\W:显示当前所在目录的最后一个目录
	\$:提示符。如果是root用户显示提示符为“#”,如果是普通用户会显示提示符为“$”

Example 1: echo $ PS1 command to view the value of the PS1 environment variable

Example 2: PS1 = '[\ u @ \ A \ w] \ $' command , modify the value of environment variable PS1

 

Language variables Bash Variables

That is the current system used by languages ​​such as Chinese, English and so on.

1, the current query language

locale
	查看当前系统语系
		LANG:定义系统主语系的变量
		LC_ALL:定义整体语系的变量

Example 1: locale command , view the current system language.

2, language variables LANG

echo $LANG
	查看系统当前语系
locale -a | more
	查看Linux支持的所有语系

Example 1: echo $ LANG command , view the current system language.

Example 2: locale -a | more command to view all languages supported by Linux.

3, query system default language

cat /etc/sysconfig/i18n
	查询系统默认语系

Echo $ LANG language which is currently running environment, and cat / etc / sysconfig / il8n is the next power of language.

Example 1: cat / etc / sysconfig / i18n command , view the system default language.

4, Linux Chinese support

Prerequisite, correct installation of Chinese fonts and Chinese languages.

Under what circumstances show Chinese

  • If you have a graphical interface that can support Chinese display correctly

  • If you use a third-party remote tools, as long as the language is set correctly, it can support Chinese display, such as XShell

  • If you use pure character interface, you must use third-party plug-ins (such as zhcon, etc.)

 

Location parameter variables Bash Variables

1, the position of the variable parameter

The main parameters are passed to the script you want to use, the defect is not suitable for third-party users, because in addition to scripters other people do not know there needs to pass several parameters.

variable meaning
$0 The current name of the script file
$n Arguments passed to the script or function. n is a number that represents the number of parameters. For example, the first parameter is $ 1, $ 2 is the second parameter.
$# The number of arguments passed to the script or function.
$* All parameters passed to the script or function.
$@ All parameters passed to the script or function. When the double quotes ( "") contains, and $ * is slightly different, the following will be mentioned.

Example 1: the n-$ , create a shell script, enter the following.

#!/bin/bash

#$0表示命令本身
order=$0
#$1表示输入的第一个参数
num1=$1
#$2表示输入的第二个参数
num2=$2

#打印
echo $order
echo $num1
echo $num2

Run the script, print results are as follows:

Example 2: $ * and $ @

$ * And $ @ command must be in double quotes ( "") cause to be effective.

If, after running test.sh 1 2 3, "$ *" to "123" (together wrap marks), "$ @" is "1", "2", "3" (respectively in quotation marks)

$ * Loop

#!/bin/bash

m=0
for i in $*
 do
  echo "参数$i"
  m=$(($m+1))
 done
echo "循环次数:$m"

"$ *" Cycle

Note that is enclosed in double quotes.

#!/bin/bash

m=0
for i in "$*"
 do
  echo "参数$i"
  m=$(($m+1))
 done
echo "循环次数:$m"

$ @ Cycle

#!/bin/bash

m=0
for i in $@
 do
  echo "参数$i"
  m=$(($m+1))
 done
echo "循环次数:$m"

"$ @" Loop

Note that in double quotes.

#!/bin/bash

m=0
for i in "$@"
 do
  echo "参数$i"
  m=$(($m+1))
 done
echo "循环次数:$m"

Summary: $ *, $ @, "$ @" are loop n times, and "$ *" is treated as a string handling, cycle only once.

Example 3: $ #

#!/bin/bash

echo "参数个数是:$#"

 

Predefined variables Bash Variables

1, predefined variables

Predefined variables effect
$? Return status of the command last executed. If this variable is 0, the proof of a command executed correctly; if this variable is non-zero (which specific number, by the command to decide), it proves a command is not correct.
$$ The current process process ID (PID)
$! The last process ID of a process running in the background (PID)

 

2, to receive keyboard input

read [选项] [变量名]
	选项:
		-p “提示信息”,在等待read输入时,输出提示信息
		-t 秒数,read命令由于会一直等待用户输入,使用此选项可以指定等待时间
		-n 字符数,read命令只接受指定的字符数,就会执行
		-s 隐藏输入的数据,适用于机密信息的输入

Example 1: read -p command , print message.

#!/bin/bash

#read -p命令输出提示信息
read -p "请输入您的名字:" name
#打印变量的值
echo "$name,您好"

Example 2: Read -t 20 , 20 seconds later 20 represents, in seconds. If the user does not enter information after 20 seconds, it will automatically continue.

#!/bin/bash

#read -p命令输出提示信息,并且等待20秒
read -p "请输入您的名字:" -t 20  name
#打印变量的值
echo "$name,您好"

Example. 3: Read -n 1 , showing a couple of characters allowed, the latter can be changed to other numeral 1, 1 denotes a character input only.

#!/bin/bash

#read -p命令输出提示信息,并且等待20秒
read -p "请输入您的性别:" -t 20 -n 1 sex
#打印变量的值
echo -e "\n"
echo "$sex"

Example. 4: Read the passwd -s , represents the input to hide the contents.

#!/bin/bash

#read -p命令输出提示信息,并且等待20秒
read -p "请输入您的密码:" -t 20 -n 6 -s passwd
#打印变量的值
echo -e "\n"
echo "密码:$passwd"

 

Published 500 original articles · won praise 77 · views 160 000 +

Guess you like

Origin blog.csdn.net/cnds123321/article/details/105022731