筆者今天寫了一個shell script,但願簡化登陸代理服務器的問題,但是script寫好以後,卻發現沒有按照預想的狀況下獲得環境變量。html
爲了讓你們好理解,貼出script的內容給各位參閱:shell
vim proxy_http.sh
加入以下配置:vim
#!/bin/bash default_domain=cmdschool default_username=will read -p "Please enter you domain(default value is cmdschool): " domain read -p "Please enter your username(default value is will): " username read -s -p "Please enter your password: " password if [ domain=="" ]; then domain=$default_domain fi if [ username=="" ]; then username=$default_username fi export http_proxy="http://$domain\\$username:$password@10.168.2.147:8080" export https_proxy="http://$domain\\$username:$password@10.168.2.147:8080"
按照平時的執行方法,咱們一般是:bash
chmod 770 proxy_http.sh ./proxy_http.sh
等同於如下執行效果服務器
sh ./proxy_http.sh
等同於如下執行效果dom
bash ./proxy_http.sh
而後你檢查環境變量,
ide
echo $http_proxy echo $https_proxy
結果發現輸出的都是空值,
spa
因而乎一頓百度,結果發現相似的帖子也有,但帖子上也沒有找到答案(帖子的問法是export如何在當前console生效),.net
http://bbs.chinaunix.net/thread-3777848-1-1.html 代理
而後聯繫了一位開源界的前輩(我也不知道對方是否願意我透露他的大名,在此暫時不透露),並獲得對方熱情指點,解決方法極其簡單,
source proxy_http.sh
因此,source指令和sh(bash)指令的區別顯而易見,就是一個不會派生子shell和一個會派生子shell,咱們平時使用source來導入環境變量,但卻沒有注意到source其實他的本質是用來執行腳本。O(∩_∩)O哈哈~。