关于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 $? 最后运行 ...
随机推荐
- 开发工具之Vscode编辑器
Visual Studio Code(以下简称vscode)是一个轻量且强大的代码编辑器,支持Windows,OS X和Linux.内置JavaScript.TypeScript和Node.js支持, ...
- laravel 开启定时任务需要操作
1.在xshell 中 crontab -e //编辑任务crontab -l //查看执行中的任务列表 2.在打开的任务中: /home/wwwroot/default 换为自己项目的根路径 vag ...
- element vue 表格编辑
https://xuliangzhan.github.io/vue-element-extends/#/editable/click1
- IP地址、子网掩码、默认网关是什么意思?
(一) 问题解析 001. 问: IP地址,子网掩码,默认网关,DNS服务器,有什么区别呀?我知道没有IP地址就不能上网,我也知道没设DNS就不能上外网,可它们都有什么功能,有什么区别呢?还有 ...
- HDU1166-ZKW树
单点修改,区间求和 // // Created by helica on 2018/3/18. // //zkw #include <cstdio> #include <cstrin ...
- 多线程threading
threading用于提供线程相关的操作,线程是应用程序中工作的最小单元.python当前版本的多线程库没有实现优先级.线程组,线程也不能被停止.暂停.恢复.中断. 1. threading模 ...
- 稍微记录下Django2.2使用MariaDB和MySQL遇到的坑
现在演示一下整个流程吧 1.创建项目和应用 PS:你也可以使用PyCharm直接创建项目 2.注册应用 先把刚刚创建的应用添加进去 3.配置MySQL或者MariaDB 4.PyMySQL替换默认的M ...
- 微信公众号开发之access_token的全局共用
最近做微信公众号开发,涉及到access_token的缓存问题(避免各自的应用都去取access_token,同时解决微信 appid和appsecret的安全问题),在通用权限管理系统底层增加了实现 ...
- 洛谷P3327 约数个数和 结论+莫比乌斯反演
原题 就是让你求\(\sum\limits_{i=1}\sum\limits_{j=1}d(ij)\)(其中\(d(x)\)表示\(x\)的因数个数) 首先有引理(然而并没有证明): \(d(ij)= ...
- SQL SERVER 2008远程数据库移植到本地的方法
https://blog.csdn.net/wuzhanwen/article/details/77449229 Winform程序或网站后台的SQL SERVER 2008放置在远程服务器上,用Mi ...