Shell 变量赋值的详细使用

一、变量的定义:

变量名的命名规则:

  1. 命名的名字要有意义
  2. 字母、数字、下划线
  3. 不以数字开头

二、变量的赋值:

变量赋值的过程,称为变量替换

1.变量名=变量值

a=123

# 将123数字赋值给a变量
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# a=123
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $a
123
2.使用let为变量赋值:

let a=10+20

# 使用let将运算结果赋值给变量a
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# let a=10+20
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $a
30

注意:shell 脚本运算的性能会很低

3.将命令赋值给变量:

l=ls

# 将ls命令赋值给变量l
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# l=ls
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $l
ls
4.将命令结果赋值给变量,使用**$()或者``**:

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
5.变量值有空格等特殊字符可以包含在 " " 或 ’ ’ 中
# 使用"" 符号将值含有空格或特殊符号进行包含起来
[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

三、变量的引用:

1.${变量名}称作对变量的引用:
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# string1="hello bash"
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo ${string1}
hello bash
2.echo ${变量名}查看变量的值
3. 变 量 名 在 部 分 情 况 下 可 以 省 略 为 {变量名}在部分情况下可以省略为 变量名:
[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.变量默认的作用范围:
# 将数字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
2.变量的导出:
  • 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
    
3.变量的删除:
  • unset

    [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# unset demo_val
    # 将demo_val变量进行删除, 再次输入结果为空
    [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# ./test.sh
    
    # 结果为空
    [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $demo_val
    
    

五、系统环境变量:

环境变量:每个Shell打开都可以获得变量

1.set和env命令:

使用env查看:

# 查看系统的默认环境变量:
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# env | more

# 查看当前用户的环境变量:
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $USER
root

使用set查看更多的环境变量:

# 可查看预定义变量和位置变量
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# set | more
2.$? $$ $0 预定义变量:

&?: 表示上一个命令是否产生错误

# 编写一个错误的命令
[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
3.$PATH:
# 查看搜索路径:
[root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

**修改 P A T H 搜 索 路 径 ∗ ∗ ( 只 对 当 前 的 PATH搜索路径**(只对当前的 PATHPATH生效):

  1. 查看当前的搜索路径有哪些:

    [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
    
  2. 在/home/lvhuiqi/目录下编写shell脚本:

    vim test.sh
    #! /bin/bash
    
    cd /home/lvhuiqi/
    # 查看当前目录下的磁盘空间
    du -sh *
    
  3. 执行编写的shell脚本:

    [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# test.sh
    bash: test.sh: command not found
    
  4. 将/home/目录的添加到搜索路径中:

    [root@iZbp1e44zna0rzyna76eszZ lvhuiqi]# PATH=$PATH:/home/
    
  5. 再次执行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
    
  6. 以上就是执行的sh当成命令来对待

  7. 再次打开一个新的终端就会发现,上面改的$PATH已经不生效:

    [root@iZbp1e44zna0rzyna76eszZ ~]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
    
4.变量位置:

$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),通用的配置文件

~用户家目录的配置文件,特有的用户配置文件

1./etc/profile:
  • 系统启动或终端启动的环境配置文件
2./etc/profile.d/:
  • 基于不同的shell脚本与类型,执行不同的脚本
3.~/bash_profile:
4.~/bashrc:
5./etc/bashrc:

注意:要是修改环境变量的配置文件,需要使用source命令让修改的配置文件立即生效

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐