Shell basics 2-variables

variable

Variables are derived from mathematics and are abstract concepts that can store calculation results or represent values ​​in computer languages. Variables can be accessed by variable name, and the accessed value is variable

  • Cannot start with a number (the standard specification is uppercase)
  • The default type of the variable value is a string type, and the specific use needs to be converted
  • There can be no spaces around the variable (for the sake of beauty)
  • Use double quotes if there are spaces
  • Variables are assigned multiple times, the last one shall prevail

Classification of variables

  • User-defined variable: Name: Custom Function: Custom Content: Custom

  • Environment variables: used for data related to the system environment

    • User-defined environment variables: Name: Customization Function: Customization Content: Customization
    • The system comes with environment variables: name: confirm the role: confirm the content: custom
  • Positional parameter variables: used for scripts to pass parameters

    • Name: Determine Role: Determine Content: Custom
  • Predefined variables: bash has defined variables

    • Name: Determine Role: Determine Content: Custom

User-defined variables

name=test
#创建自定义变量
name=test1
#修改也就是覆盖变量值
$name
#变量调用
unset name
#删除变量
set -u
#查看未定义过变量名,值为空,而如果定义为空,这样就无法区分,使用set -u可以区分,变量不存在就报错

Environment variable

环境变量在父shell和子shell中都是生效的,而用户自定义只在当前shell下

User-defined environment variables

export name=test
#就是基于用户自定义加了export
#set 查看所有变量
#env 查看环境变量

System comes with environment variables

env

LANG=en_US.UTF-8   #语言
HISTCONTROL=ignoredups
HOSTNAME=host138   #主机名
OLDPWD=/bin   
XDG_SESSION_ID=3
USER=root      #用户名
PWD=/root 
HOME=/root
SSH_CLIENT=192.168.26.1 63647 22
SSH_TTY=/dev/pts/0
MAIL=/var/spool/mail/root
SHELL=/bin/bash  #shell类型
TERM=x term
SHLVL=2
LOGNAME=root
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus
XDG_RUNTIME_DIR=/run/user/0
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#系统命令查找路径
HISTSIZE=1000  #历史命令记录数
LESSOPEN=||/usr/bin/lesspipe.sh %s
_=/usr/bin/env
#系统自带环境变量都是会影响到系统正常运行

PS1 environment variables (command prompt)
Insert picture description hereInsert picture description here

vi /etc/bashrc
#永久修改PS1环境变量

Linguistic variables

[root@host138 ~]# echo $LANG
en_US.UTF-8
#保存的是当前的操作语系
[root@host138 ~]# locale -a | wc -l
865
#查看系统支持的语言

[root@host138 ~]# locale 
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
#查看系统已经安装的语言

Positional parameter variable

  • $n: n is a number, 0 stands for itself, $ 1-9 stands for the first to ninth parameters, 9 stands for the first to ninth parameters for parameters above 10, and uses for parameters above 109 Generation Table first a th to the first nine th parameter numbers , . 1 0 to on the reference number with {10}(common)
  • ∗: represents all the parameters in the command, *: represents all the parameters in the command, * : Generation table command order in the as there are parameter numbers , * all parameters as a whole
  • $@: represents all the parameters in the command, each parameter is treated differently and belongs to a single ** (commonly used) **
  • $#: the number of statistical parameters
#!bin/bash
a=$1
b=$2

echo $a 
echo $b

[root@host138 ~]# bash test.sh 1 2
1
2
#传递2个参数给脚本,这种方式往往只适合写脚本的本人使用,因为别人不知道需要输入几个参数,以及各式是什么

#!bin/bash

echo $*
echo $*
echo $#
[root@host138 ~]# bash test.sh 1 2 3 4 5
1 2 3 4 5  #代表是一个整体
1 2 3 4 5  #每个数值都是单个整体
5  #参数个数
#但是缺陷就是其他用户无法知道需要输入什么类型的值

Predefined variables

  • $? The return value of the last run command, 0 means correct, other values ​​are incorrect
  • $$ Process ID of the current process
  • $! The last process number running in the background
    Insert picture description here

read receives keyboard output

Positional parameter variables have many defects for other users. It's not that the writer can't judge the number and type of parameters, but read has added interactive input, and the user can input according to the prompts.

  • -p The prompt message waiting for input, you can specify what information is required to be output
  • -t The number of seconds to wait for user input
  • -n Enter a character, press Enter to confirm
  • -s hide the entered content, suitable for entering security information such as passwords
#!bin/bash

read -t 30 -p "Please enter whole number:" num1
read -t 30 -p "Please enter password :" -s num2

echo -e "\n$num1"
echo $num2

结果
[root@host138 ~]# bash test.sh 
Please enter whole number:11111
Please enter password :
11111
22222

Operator

declare declare variable type

  • -Set variable type
  • + Remove variable type
  • -i is declared as an integer
  • -r statement is read-only, and cannot be modified or cancelled
  • -x is declared as an environment variable
  • -p displays the type of declared variable
  • -a is declared as an array type (that is, the meaning of a list)

But there are still a lot of types that are not supported. The date type, puppet type, floating point type, etc.
declare have the shortcomings that it is difficult to use. Not recommended

$((operation))

Insert picture description here

The default variables are all characters and cannot be operated on

Insert picture description here

Calculate by $((calculation)), it is recommended to use

Operation priority (the larger the priority, the higher the priority)
Insert picture description here

It is also not recommended, you can adjust the priority directly through parentheses ()
Insert picture description here

Environment variable configuration file

source configuration file

Environment variable configuration file in the home directory (valid for the current shell)

  • .bash_profile
  • .bashrc

Environment variable configuration file in the etc directory (all shells take effect)

  • /etc/profile.d/*.sh
  • /etc/profile
  • /etc/bashrc

Configuration file calling process
Insert picture description here

Environment variable process loaded after user login

Insert picture description here

Non-login situation, su or subshell

Exit the effective environment variable file

  • .bash_logout

The default is empty, you can configure the function according to your needs. The
system needs to execute logout to take effect.

Guess you like

Origin blog.csdn.net/yangshihuz/article/details/110850509