shell脚本中的变量:
- 只能在函数内部使用的是局部变量,一般用local修饰。
- 只能在当前shell进程中使用的是全局变量,其作用域从被定义的地方开始,到shell结束或被显示删除的地方为止。
- 可以在子进程中使用的变量是环境变量,一般用export导出。
- shell函数定义的变量默认是全局变量。
- 函数的参数是local的。
- 同名情况下,函数定义的local变量会屏蔽脚本定义的全局变量。
- 环境变量只能从父进程传给子进程,不能从子进程传递给父进程。
source、sh、./的区别
- sh filename.sh与./filename.sh没有区别,都是重新建立一个子进程,这个子进程会继承父进程的环境变量,但是父进程中的定义全局变量不会带到子进程,子进程中的定义的全局变量也不会被带回父shell。
- source filename.sh 只是简单地读取脚本里面的语句依次在当前shell里面执行,没有建立新的子进程。因此两个文件中的全局变量都是共享的。