Understand shell variables in Linux

#####Check the shell category under Linux: cat /etc/shells image.png
#⚪bash
------The shell used by most Linux systems by default, bash Shell is a free version of Bourne Shell, it is the earliest Unix Shell, bash also has a feature, you can view the help through the help command. The included functions can almost cover the functions of Shell, so the general Shell script will specify it as the execution path

#一.bash's initialization
#####1, /etc/profile
global (public) configuration, no matter which user it is, the file will be read when logging in.
#####2, /ect/bashrc
Ubuntu does not have this file, and the corresponding is /ect/bash.bashrc It is also a global (public) bash execution, no matter how it is, it will read this file.
#####3, ~/.profile
If bash is executed in login mode, read ~/.bash_profile, if it does not exist, read ~/.bash_login, if the first two do not exist, read ~ /.profile. When logging in in graphical mode, this file will be read even if there are ~/.bash_profile and ~/.bash_login.
#####4, ~/.bash_login
If bash is executed in login mode, read ~/.bash_profile, if it does not exist, read ~/.bash_login, if the first two do not exist, read ~ /.profile.
#####5, ~/.bash_profile
Unbutu does not have this file by default, and it can be created. This file will only be read when bash is executed in the form of login. Usually the configuration file is also configured to read ~/.bashrc.
#####6, ~/.bashrc
When bash is executed in non-login form, read this file. If it is executed in login form, this file will not be read.
#####7, ~/.bash_logout
This file will only be read when logging off, and in longin form. When logging out in text mode, this file will be read, and when logging out in graphics mode, this file will not be read.
#二.bash's features
####1. Command and file auto-completion-
Linux command auto-completion requires bash-completion to be installed
image.png
---- After installation, restart the system to normal tab completion
####2 .Command history memory function

1.查看之前使用过的所有命令:history
2.显示最近的n个命令:history n
3.删除相应的第n个命令:history -d n
4.指定执行命令历史中的第n条语句:!n
5.指定执行命令历史中倒数第n条语句:!-n
6.执行命令历史中最后一条语句:!!
7.执行命令历史中最近一条以[String]开头的语句:![String]
8.引用上一个命令中的最后一个参数:!$
----COMMAND + Esc键 + . 输入COMMAND之后,按下Esc键,松开后再按 . 则可以自动输入最近一条语句使用的参数 
----COMMAND + Alt + . 输入COMMAND之后,同时按下Alt和. 键,也可以自动输入最近一条语句使用的参数
9.将历史命令写入命令历史的文件中:history -w
10.回显 echo 之后的语句,而使用 echo $FILENAME 命令可以查看该 file 所在的路径:echo $HISTFILE
11.查看命令历史的内容: cat .bash_history
12.删除所有的命令历史记录:history -c

####3. Alias ​​function

1.查看系统当前所有的别名:alias
2.定义新的别名,输入h5就相当于'head-5'
3.取消定义的别名:unalias h5

####4. Commonly used shortcut keys
----ctrl +A: move the cursor to the beginning of the command line. If the command we entered is too long, we want to move the cursor to the beginning of the command line.
----ctrl +E: move the cursor to the end of the command line
----ctrl +C: forcibly terminate the current command
#二.shell variables
#####1. Meaning:
use a fixed string to indicate Unfixed content

#####2. Variable type:
self-defining environment variable:

  1. Method one export back_dir2=/home/backup
  2. Method two export back_dir1 to convert custom variables into environment variables.
    Reference environment variables: variable name or {variable name} to
    view environment variables: echo $variable name env For example, env | grep back_dir2
    cancel environment variable: unset variable name
    variable scope: current Shell and sub
  3. Location variable
    $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}

  4. $0 脚本名
    $* 所有的参数
    $@ 所有的参数
    $# 参数的个数
    $$ 当前进程的PID
    $! 上一个后台进程的PID
    $? 上一个命令的返回值 0表示成功
    The difference between predefined variables #####3.* and @
# vim ping.sh 
#!/bin/bash 
ping ‐c2 $1 &>/dev/null 
if [ $? = 0 ];then 
echo "host $1 is ok" 
else
echo "host $1 is fail" 
fi
[root@qfdeu ~]# chmod a+x ping.sh 
[root@qfdeu ~]# ./ping.sh 192.168.2.25

#####4. Variable assignment method
######① Explicit assignment
variable name = variable value

ip1=192.168.1.251
school="BeiJing 1000phone"
today1=`date +%F`
today2=$(date +%F)

######②read reads the variable value from the keyboard
read variable name
read -p "prompt message: "variable name
read -t 5 -p "prompt message:" variable name
• -t followed by the number of seconds to define the input character The waiting time of
read -n 2 Variable name
• -n followed by a number, defines the length of the input text, very practical.

[root@qfdeu ~]# vim first.sh
back_dir1=/var/backup
read ‐p "请输入你的备份目录: " 
back_dir2 echo $back_dir1 echo 
$back_dir2 [root@qfdeu ~]# sh first.sh
[root@qfdeu ~]# vim ping2.sh 
#!/bin/bash
 read ‐p "Input IP: " ip
 ping ‐c2 $ip &>/dev/null
 if [ $? = 0 ];then 
echo "host $ip is ok" 
else
echo "host $ip is fail" 
fi
[root@qfdeu ~]# chmod a+x ping2.sh 
[root@qfdeu ~]# ./ping.sh

#####5. Define reference variables
----"" Weak Reference
---- " Strong Reference

[root@qfdeu ~]# school=1000phone 
[root@qfdeu ~]# echo "${school} is good" 
1000phone is good
 [root@qfdeu ~]# echo '${school} is good' 
${school} is good

----(Backquote) command substitution is equivalent to the shell command in the $() backquote will be executed first

[root@qfdeu ~]# touch `date +%F`_file1.txt 
[root@qfdeu ~]# touch $(date +%F)_file2.txt 
[root@qfdeu ~]# disk_free3="df ‐Ph |grep '/$' |awk '{print $4}'" # 错误 
[root@qfdeu ~]# disk_free4=$(df ‐Ph |grep '/$' |awk '{print $4}') 
[root@qfdeu ~]# disk_free5=`df ‐Ph |grep '/$' |awk '{print $4}'`

#####6. Deletion and replacement of variable "content"
######① Deletion of "content"

[root@qfedu ~]# url=www.sina.com.cn
 [root@qfedu ~]# echo ${#url} # 获取变量值的长度 
15
[root@qfedu ~]# echo ${url} # 标准查看 www.sina.com.cn 
[root@qfedu ~]# echo ${url#*.} # 从前往后,最短匹配 
sina.com.cn 
[root@qfedu ~]# echo ${url##*.} # 从前往后,最长匹配 贪婪匹配 
cn
[root@qfedu ~]# url=www.sina.com.cn 
[root@qfedu ~]# echo ${url} www.sina.com.cn 
[root@qfedu ~]# echo ${url%.*} # 从后往前,最短匹配 
www.sina.com 
[root@qfedu ~]# echo ${url%%.*} # 从后往前,最长匹配 贪婪匹配 
www 
[root@qfedu ~]# url=www.sina.com.cn 
[root@qfedu ~]# echo ${url#a.}
www.sina.com.cn 
[root@qfedu ~]# echo ${url#*sina.} com.cn
[root@qfedu ~]# echo $HOSTNAME qfedu.1000phone.com 
[root@qfedu ~]# echo ${HOSTNAME%%.*} 
qfedu

######②Replacement of "Content"

[root@qfedu ~]# url=www.sina.com.cn
[root@qfedu ~]# echo ${url/sina/baidu}
www.baidu.com.cn 
[root@qfedu ~]# url=www.sina.com.cn 
[root@qfedu ~]# echo ${url/n/N}
 www.siNa.com.cn 
[root@qfedu ~]# echo ${url//n/N} 贪婪匹配 
www.siNa.com.cN

######③Substitution of variables

[root@qfedu ~]# unset var1
[root@qfedu ~]# 
[root@qfedu ~]# echo ${var1} 
[root@qfedu ~]# echo ${var1‐aaaaa}
 aaaaa 
[root@qfedu ~]# var2=111 
[root@qfedu ~]# echo ${var2‐bbbbb}#####Linux下查看shell的类别:cat /etc/shells![image.png](https://upload-images.jianshu.io/upload_images/24318310-653bdee7f795a9a2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#⚪bash
------大多数Linux系统默认使用的Shell,bash Shell是Bourne Shell 的一个免费版本,它是最早的Unix Shell,bash 还有一个特点,可以通过help命令 来查看帮助。包含的功能几乎可以涵盖Shell所具有的功能,所以一般的Shell脚本都会指定它为执行路径


#一.bash的初始化
#####1、/etc/profile
 全局(公有)配置,不管是哪个用户,登录时都会读取该文件。
 #####2、/ect/bashrc
Ubuntu 没有此文件,与之对应的是 /ect/bash.bashrc 它也是全局(公有)的 bash 执行时,不管是何种方式,都会读取此文件。 
#####3、~/.profile 
若 bash 是以 login 方式执行时,读取 ~/.bash_profile,若它不存在,则读取 ~/.bash_login,若前两者不存 在,读取~/.profile。 图形模式登录时,此文件将被读取,即使存在 ~/.bash_profile 和 ~/.bash_login。 
#####4、~/.bash_login
 若 bash 是以 login 方式执行时,读取 ~/.bash_profile,若它不存在,则读取 ~/.bash_login,若前两者不存 在,读取 ~/.profile。
#####5、~/.bash_profile 
Unbutu 默认没有此文件,可新建。 只有 bash 是以 login 形式执行时,才会读取此文件。通常该配置文件还会配置成去读取 ~/.bashrc。 
#####6、~/.bashrc 
当 bash 是以 non-login 形式执行时,读取此文件。若是以 login 形式执行,则不会读取此文件。 
#####7、~/.bash_logout 
注销时,且是 longin 形式,此文件才会读取。在文本模式注销时,此文件会被读取,图形模式注销时,此文 件不会被读取。
#二.bash的特性
####1.命令和文件自动补齐
----Linux命令自动补全需要安装bash-completion
![image.png](https://upload-images.jianshu.io/upload_images/24318310-c0b68952e51d0fc7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
----安装后重启系统可正常tab补齐
####2.命令历史记忆功能

1. View all the commands used before: history
2. Display the most recent n commands: history n
3. Delete the corresponding nth command: history -dn
4. Specify the nth statement in the command history:! n
5. Specify the last nth statement in the execution command history:! -n
6. Execute the last statement in the command history :! !
7. Execute the most recent statement beginning with [String] in the command history:! [String]
8. Quote the last parameter in the previous command:! $
----COMMAND + Esc +. After entering COMMAND, press Esc, release and press again. Then you can automatically enter the parameters used in the last sentence
----COMMAND + Alt +. After entering COMMAND, at the same time Press the Alt and. Keys, you can also automatically enter the parameters used in the most recent statement
9. Write historical commands into the command history file: history -w
10. Echo the statements after echo, and use the echo $FILENAME command to view The path where the file is located: echo $HISTFILE
11. View the contents of the command history: cat .bash_history
12. Delete all the command history records: history -c

####3.别名功能

1. View all current aliases in the system: alias
2. Define a new alias, input h5 is equivalent
to'head -5' 3. Undefine the alias: unalias h5

####4.常用的快捷键
----ctrl +A:把光标移动到命令行开头。如果我们输入的命令过长,想要把光标移动到命令行开头时使用。
----ctrl +E:把光标移动到命令行结尾
----ctrl +C:强制终止当前的命令
#二.shell 变量
#####1.含义:
用一个固定的字符串去表示不固定的内容

#####2.变量的类型:
自义环境变量: 
1. 方法一 export back_dir2=/home/backup 
2. 方法二 export back_dir1 将自定义变量转换成环境变量
 引用环境变量:变 量 名 或 {变量名}
 查看环境变量:echo $变量名 env 例如 env | grep back_dir2
 取消环境变量:unset 变量名
 变量作用范围:在当前Shell和子
3. 位置变量
		$1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
4. 预定义变量
        ``$0    脚本名``
	``$*		所有的参数``
	``$@ 	所有的参数``
	``$# 	参数的个数``
	``$$ 	当前进程的PID``
	``$!     上一个后台进程的PID``
	``$?		上一个命令的返回值 0表示成功``
#####3.*和@的区别

vim ping.sh

#!/bin/bash
ping ‐c2 $1 &>/dev/null
if [ $? = 0 ];then
echo “host $1 is ok”
else
echo “host $1 is fail”
fi
[root@qfdeu ~]# chmod a+x ping.sh
[root@qfdeu ~]# ./ping.sh 192.168.2.25

#####4.变量的赋值方式
######①显式赋值
变量名=变量值

ip1=192.168.1.251
school=“BeiJing 1000phone”
today1=date +%F
today2=$(date +%F)

######②read从键盘读入变量值
read 变量名
		read -p "提示信息: "  变量名
		read -t 5 -p "提示信息: "  变量名
		• -t 后面跟秒数,定义输入字符的等待时间
		read -n 2 变量名
		• -n 后跟一个数字,定义输入文本的长度,很实用。

[root@qfdeu ~]# vim first.sh
back_dir1=/var/backup
read ‐p "Please enter your backup directory: "
back_dir2 echo $back_dir1 echo
$back_dir2 [root@qfdeu ~]# sh first.sh

[root@qfdeu ~]# vim ping2.sh
#!/bin/bash
read ‐p "Input IP: " ip
ping ‐c2 $ip &>/dev/null
if [ $? = 0 ];then
echo “host $ip is ok”
else
echo “host $ip is fail”
fi
[root@qfdeu ~]# chmod a+x ping2.sh
[root@qfdeu ~]# ./ping.sh

#####5.定义引用变量
----""  弱引用
----''  强引用

[root@qfdeu ~]# school=1000phone
[root@qfdeu ~]# echo "KaTeX parse error: Expected 'EOF', got '#' at position 53: … [root@qfdeu ~]#̲ echo '{school} is good’
${school} is good

----(反引号)命令替换 等价于 $() 反引号中的Shell命令会被先执行

[root@qfdeu ~]# touch date +%F_file1.txt
[root@qfdeu ~]# touch KaTeX parse error: Expected 'EOF', got '#' at position 37: … [root@qfdeu ~]#̲ disk_free3="df…’ |awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 4}̲'" # 错误 [root@…(df ‐Ph |grep ‘/$’ |awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 4}̲') [root@qfdeu…’ |awk ‘{print $4}’`

#####6.变量"内容"的删除和替换
######①"内容"的删除

[root@qfedu ~]# url=www.sina.com.cn
[root@qfedu ~]# echo ${#url} # Get the length of the variable value
15
[root@qfedu ~]# echo ${url} # Standard Check www.sina.com.cn
[root@qfedu ~]# echo ${url# .} # From the front, the shortest match
sina.com.cn
[root@qfedu ~]# echo ${url##
.} # From front to back, the longest match greedy match
cn
[root@qfedu ~]# url=www.sina.com.cn
[root@qfedu ~]# echo ${url} www.sina.com.cn
[root@qfedu ~ ]# echo ${url%. } # From back to front , the shortest match
www.sina.com
[root@qfedu ~]# echo ${url%%.
} # From back to front , the longest match greedily matches
www.
[ root@qfedu ~]# url=www.sina.com.cn
[root@qfedu ~]# echo ${url#a.}
www.sina.com.cn
[root@qfedu ~]# echo ${url# sina .} com.cn
[root@qfedu ~]# echo $HOSTNAME qfedu.1000phone.com
[root@qfedu ~]# echo ${HOSTNAME%%.
}
qfedu

######②"内容"的替换

[root @ qfedu] # url = www.sina.com.cn
[root q qfedu ~] # echo $ url / sina / baidu}
www.baidu.com.cn
[root @ qfedu ~] # url = www. sina.com.cn
[root @ qfedu ~] # echo $ url / n / N}
www.siNa.com.cn
[root q qfedu ~] # echo $ {url // n / N} 匹配
www.siNa .com.cN

######③变量的替代

[root@qfedu ~]# unset var1
[root@qfedu ~]#
[root@qfedu ~]# echo ${var1}
[root@qfedu ~]# echo ${var1‐aaaaa}
aaaaa
[root@qfedu ~]# var2=111
[root@qfedu ~]# echo ${var2‐bbbbb}
111
[root@qfedu ~]#
[root@qfedu ~]# var3=
[root@qfedu ~]# echo ${var3‐ccccc}

 111 
[root@qfedu ~]# 
[root@qfedu ~]# var3= 
[root@qfedu ~]# echo ${var3‐ccccc}

Guess you like

Origin blog.csdn.net/weixin_49844466/article/details/107846972