关于shell变量的继承总结
结论:
默认,父shell和子shell的变量是隔离的。
sh方式运行脚本,会重新开启一个子shell,无法继承父进程的普通变量,能继承父进程export的全局变量。
source或者. 方式运行脚本,会在当前shell下运行脚本,相当于把脚本内容加载到当前shell后执行,自然能使用前面定义的变量。
验证:在子shell中调用父shell普通变量
[root@gjt scripts]# echo $b [root@gjt scripts]# echo $a [root@gjt scripts]# b=gaga
[root@gjt scripts]# echo $b
gaga
[root@gjt scripts]# cat test1.sh
a=haha
echo "test1: $a"
echo "test1: $b"
sh /root/scripts/test2.sh
[root@gjt scripts]# cat test2.sh
echo "test2:$a"
echo "test2:$b"
[root@gjt scripts]# sh test1.sh
test1: haha
test1:
test2:
test2:
#执行过程解释:
sh test1.sh ==>重新启动一个子shell
a=haha ==>a变量赋值
echo "test1: $a" ==>输出:test1: haha
echo "test1: $b" ==>输出:test1: 因为子shell不会继承父shell的普通变量,所以$b为空
sh /root/scripts/test2.sh ==>重新启动一个子shell
echo "test2:$a" ==>输出:test2: 同上,$a为空
echo "test2:$b" ==>输出:test2: 同上,$b为空 [root@gjt scripts]# source test1.sh
test1: haha
test1: gaga
test2:
test2:
[root@gjt scripts]# echo $a
haha
#执行过程解释:
source test1.sh ==>在当前shell下执行脚本
a=haha ==>a变量赋值
echo "test1: $a" ==>输出:test1: haha
echo "test1: $b" ==>输出:test1: gaga 在运行脚本之前在终端定义了b变量。
sh /root/scripts/test2.sh ==>重新启动一个子shell
echo "test2:$a" ==>输出:test2: $a未定义
echo "test2:$b" ==>输出:test2: $b未定义 [root@gjt scripts]# echo $a ==>输出:haha,source test1.sh时定义了。
验证:在子shell中调用父shell普通变量
验证:在子shell中调用父shell定义的export全局变量
[root@gjt scripts]# echo $b [root@gjt scripts]# echo $a [root@gjt scripts]# cat test1.sh
export a=haha
echo "test1: $a"
echo "test1: $b"
sh /root/scripts/test2.sh
[root@gjt scripts]# cat test2.sh
echo "test2:$a"
echo "test2:$b"
[root@gjt scripts]# export b=gaga
[root@gjt scripts]# sh test1.sh
test1: haha
test1: gaga
test2:haha
test2:gaga #输出说明,父shell定义的全局变量可以传递给子shell以及子shell的子shell
验证:在子shell中调用父shell定义的export全局变量
[root@gjt scripts]# echo $b [root@gjt scripts]# echo $a [root@gjt scripts]# cat test1.sh
export a=haha
echo "test1: $a"
echo "test1: $b"
sh /root/scripts/test2.sh
[root@gjt scripts]# cat test2.sh
echo "test2:$a"
echo "test2:$b"
[root@gjt scripts]# export b=gaga
[root@gjt scripts]# sh test1.sh
test1: haha
test1: gaga
test2:haha
test2:gaga
[root@gjt scripts]# echo $a [root@gjt scripts]# #最后的$a输出为空,说明子shell运行结束后,其定义的全局变量和普通变量均自动销毁。
验证:在父shell中无法调用子shell定义的export全局变量
注意:测试过程中如果使用了source运行脚本,请退出终端或unset再进行其他测试,避免变量的值对其他验证有影响。
关于shell变量的继承总结的更多相关文章
- (转载)linux中shell变量
(转载)http://blog.csdn.net/zahuopuboss/article/details/8633891 为使shell编程更有效,系统提供了一些shell变量.shell变量可以保存 ...
- 【shell编程基础1】shell变量篇
Bash shell bash shell 是bourne shell 的升级版,“bourne again shell”.ubuntu的默认shell. 预备知识 1. "#!" ...
- Shell脚本编程(二):shell变量
定义变量 定义变量时,变量名不加美元符号($,PHP语言中变量需要),如: your_name="runoob.com" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程 ...
- linux 中的局部变量、全局变量、shell 变量的总结
系统局部变量和全局变量 一.变量分类局部变量和环境变量,局部变量只适用于当前shell,而环境变量是全局的,它适用于所有当前shell以及其派生出来的任意子进程,有些变量是用户创建的,其他的则是专用 ...
- shell变量与运算
shell变量与运算 @(0003 shell编程) 变量存在于内存中.假设变量str,设置或修改变量属性时,不带$号,只有引用变量的值时才使用$号.也就是说在内存中,标记变量的变量名称是str,而不 ...
- 二、Shell变量
类型 注释强变量 变量在使用前,必须事先声明,甚至还需要初始化 弱变量 变量用时声明,甚至不区分类型 变量的作用:用来保存变化的数据 变量名 名称固定,由系统设定或用户定义 变量值 根据用户设 ...
- Shell变量的作用域:Shell全局变量、环境变量和局部变量
Shell 变量的作用域(Scope),就是 Shell 变量的有效范围(可以使用的范围). 在不同的作用域中,同名的变量不会相互干涉,就好像 A 班有个叫小明的同学,B 班也有个叫小明的同学,虽然他 ...
- shell变量
定义变量 定义变量时,变量名不加美元符号($),如: variableName="value" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样.同时,变量名 ...
- linux中shell变量$#,$@,$0,$1,$2的含义解释
linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...
随机推荐
- 软件定义网络(Software Defined Network,SDN)简介
SDN的三大关键要素 第一关键要素是转发与控制分离,这使得网络交换机的数据转发变得更加简单.快速:同时,控制变成了网络操作系统中一个相对集中的逻辑功能. 第二个关键要素是OpenFlow协议,它向交换 ...
- python学习之时间处理
主要学习datetime,time,时区 待更新...
- MyBatis 3源码解析(二)
二.获取SqlSession对象 1.首先调用DefaultSqlSessionFactory 的 openSession 方法,代码如下: @Override public SqlSession o ...
- Redis快问快答
本随笔的回答来自 http://www.runoob.com/redis/redis-tutorial.html 另一个不错的教程: https://www.yiibai.com/redis/redi ...
- 【HTML+CSS】在书写代码时的便捷应用
创建多个相同元素: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...
- 熟悉常用的HDFS操作
编程实现以下指定功能,并利用Hadoop提供的Shell命令完成相同任务: 在本地Linux文件系统的“/home/hadoop/”目录下创建一个文件txt,里面可以随意输入一些单词. 在本地查看文件 ...
- ionic3隐藏子页面的tabs和配置返回按钮
在app.modlues.ts文件中修改 imports: [ BrowserModule, IonicModule.forRoot(MyApp, { tabsHideOnSubPages: 'tru ...
- pycharm中查找一个对象在哪里被引用
pycharm中查找一个对象在哪里被引用 2018年10月28日 19:22:20 vivian_wanjin 阅读数:1600 PyCharm的Find Usages功能可以查找某个对象(变量. ...
- React Fullpage
之前项目需要,单独拿出来做了个demo 目前仅支持收尾加autoheight github地址:https://github.com/zlinggnilz/React-Fullpage
- Git 操作命令
一.Git 基本配置 1.配置 命令:git config --global prop_name prop_value 如配置git用户名与邮箱: git config --global user. ...