在Linux系統中,咱們常須要配置環境變量甚至執行命令,好比,在 /etc/profile 中的 PATH;
在~/.bash_profile 中的 alias。shell
本文意在介紹不一樣的文件中命令的執行時機和做用範圍。bash
在Linux內核系統中,起類似做用的文件共分爲:code
/etc/profiletable
/etc/bashrc(在Ubuntu系統中爲/etc/bash.bashrc)變量
~/.bash_profile擴展
~/.bashrc配置
File Name | Login | New bash |
---|---|---|
/etc/profile | true | false |
/etc/bashrc | true | true |
~/.bash_profile | true | false |
~/.bashrc | true | true |
申明變量時,不加export只能在本shell文件內引用;加上export後,在本文件執行的全部shell中都可引用。file
a.sh:引用
#!/bin/bash echo ----- a.sh ----- A=1 export B=2 echo A=$A echo B=$B sh ./b.sh
b.sh:總結
#!/bin/bash echo ----- b.sh ----- echo A=$A echo B=$B sh c.sh
c.sh:
#!/bin/bash echo ----- c.sh ----- echo A=$A echo B=$B
在 a.sh 中申明 A B 兩個變量,其中 B 變量使用了 export。b.sh 和 c.sh 僅僅是打印 A B 變量。打印結果以下:
[root@886d89b1d8b4:/tmp# ./a.sh ----- a.sh ----- A=1 B=2 ----- b.sh ----- A= B=2 ----- c.sh ----- A= B=2
總結:使用 export 申明變量可在 bash 打開的全部 bash 中引用。