变量名的命名规则:
变量赋值的过程,称为变量替换。
a=123
# 将123数字赋值给a变量 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# a=123 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $a 123
let a=10+20
# 使用let将运算结果赋值给变量a [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# let a=10+20 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $a 30
注意:shell 脚本运算的性能会很低
l=ls
# 将ls命令赋值给变量l [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# l=ls [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $l ls
letc=$(ls -l /etc)
# 使用$()命令结果赋值给变量 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# cmd1=$(ls /home/lvhuiqi/) [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $cmd1 Dockerfile docker-compose.yml new_shell.sh requirements.txt shell_test.sh test.log test.sh
# 使用 `` 符号将命令的结果赋值给变量 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# cmd2=`ls /home/lvhuiqi/` [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $cmd2 Dockerfile docker-compose.yml new_shell.sh requirements.txt shell_test.sh test.log test.sh
# 使用"" 符号将值含有空格或特殊符号进行包含起来 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# string1="hello bash" [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $string1 hello bash
# 使用''进行处理,里面含有字符串的""双引号 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# string2='"hello bash"' [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $string2 "hello bash"
注意:赋值操作变量的等号(=)两侧不允许出现空格。
# 错误示例 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# a = 123 -bash: a: command not found
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# string1="hello bash" [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo ${string1} hello bash
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# string1="hello bash" [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $string1 hello bash
想在输出结果后加入自定义的值:
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# string1="hello bash" [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo ${string1}23 hello bash23
# 将数字1赋值给变量a [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# a=1 # bash 开启一个子进程 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# bash # 查看变量a的值,发现子进程并不会看到父进程的变量值 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $a # 在子进程的bash里面将数字2赋值给变量a [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# a=2 # 退出子进程 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# exit exit # 查看变量a发现值为父进程的值1 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $a 1
总结:变量的默认作用范围,就是bash的自身
# 使用脚本文件,来获取父进程的变量,使用source命令 # 将"hello subshell"变量赋值给demo_val [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# demo_va1="hello subshell" # 编写test.sh脚本 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# vim test.sh # 内容如下: #! /bin/bash echo $demo_val # 给test.sh赋执行权限 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# chmod u+x test.sh # 使用./test.sh bash命令来查看,可以看出没有打印值 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# ./test.sh # 使用source 可以将值进行打印 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# source ./test.sh hello subshell [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# . ./test.sh hello subshell
export,将子进程获得到父进程的变量:
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# export demo_val="hello subshell" [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# vim test.sh # 内容如下: #! /bin/bash echo $demo_val [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# ./test.sh hello subshell
unset
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# unset demo_val # 将demo_val变量进行删除, 再次输入结果为空 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# ./test.sh # 结果为空 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $demo_val
环境变量:每个Shell打开都可以获得变量
使用env查看:
# 查看系统的默认环境变量: [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# env | more # 查看当前用户的环境变量: [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $USER root
使用set查看更多的环境变量:
# 可查看预定义变量和位置变量 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# set | more
&?: 表示上一个命令是否产生错误
# 编写一个错误的命令 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# $? bash: 0: command not found # 使用$?来查看上个命令是否错误 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $? 127 # 正确的使用命令 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo 123 123 # 再次使用$?来查看上次命令是否错误,返回0表示正确 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $? 0
&&: 可检测当前进程的PID
# 检测当前进程的PID [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $$ 24764
&0:显示当前脚本的名称
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $0 bash
举个栗子:
# 创建一个shell 脚本 vim test.sh #! /bin/bash echo $$ echo $0 # 使用$$跟$0根据不同的执行命令来检查PID与Pname [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# bash test.sh 25336 test.sh [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# . ./test.sh 24764 bash
# 查看搜索路径: [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
**修改 P A T H 搜 索 路 径 ∗ ∗ ( 只 对 当 前 的 PATH搜索路径**(只对当前的 PATH搜索路径∗∗(只对当前的PATH生效):
查看当前的搜索路径有哪些:
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
在/home/lvhuiqi/目录下编写shell脚本:
vim test.sh #! /bin/bash cd /home/lvhuiqi/ # 查看当前目录下的磁盘空间 du -sh *
执行编写的shell脚本:
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# test.sh bash: test.sh: command not found
将/home/目录的添加到搜索路径中:
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# PATH=$PATH:/home/
再次执行test.sh脚本:
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# test.sh 4.0K docker-compose.yml 4.0K Dockerfile 4.0K requirements.txt 4.0K shell_test.sh 4.0K test.log 4.0K test.sh
以上就是执行的sh当成命令来对待
再次打开一个新的终端就会发现,上面改的$PATH已经不生效:
[root@iZbp1e44zna0rzyna76eszZ ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
$1 2.... 2.... 2....n
注意:${10}的位置参数书写
# 编写test.sh脚本 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# vim test.sh # 编写内容如下: #! /bin/bash pos1=$1 pos2=$2 echo $pos1 echo $pos2 # 传入位置参数 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# bash test.sh 位置参数1 位置参数2 位置参数1 位置参数2
如果传递参数为空,可以设置一个{xxx-_}:
# 编写test.sh脚本 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# vim test.sh # 编写内容如下: #! /bin/bash echo $1 echo ${2-_} # 传入位置2参数为空 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# bash test.sh 参数2传递为空 参数2传递为空 _ # 传入位置参数都不为空 [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# bash test.sh 参数1 参数2 参数1 参数2
/etc/目录下的配置文件是所有用户的权限(无论是root、还是user),通用的配置文件
~用户家目录的配置文件,特有的用户配置文件
注意:要是修改环境变量的配置文件,需要使用source命令让修改的配置文件立即生效。