Open gracekrcx opened 2 years ago
什麼是『子程序』呢?就是說,在我目前這個 shell 的情況下,去啟用另一個新的 shell ,新的那個 shell 就是子程序啦!在一般的狀態下,父程序的自訂變數是無法在子程序內使用的。但是透過 export 將變數變成環境變數後,就能夠在子程序底下應用了!
簡單來說,變數就是一個容器,裝著某些資料,而環境變數則是『無時無刻』都存在系統的背景環境中的一個容器,可以讓我們去存取裝在容器裡面的資料。
環境變數中的 PATH,代表著「系統要到哪些路徑底下找執行檔」。
Process The process object is a global that provides information about, and control over, the current Node.js process. As a global, it is always available to Node.js applications without using require(). process.env 在 NodeJS 裡拿環境變數的方式
Example:
process.env.TEST = 1;
console.log(process.env.test);
// => 1
可以在執行時,設定環境變數 (設定環境變數 PORT, NDOE_DEV)
PROT=3001 node index.js
PROT=3002 NDOE_DEV=development node index.js
Heroku dynamic port for your app to bind to.
process.env.PORT
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Our app is running on port ${ PORT }`);
});
bash shell commond line
When you run commands in your shell, you are really writing a small bit of code that your shell interprets. 當你在 shell 中執行命令時,你實際上是在寫一小段程式碼,由 shell 來解釋執行
If the shell is asked to execute a command that doesn’t match one of its programming keywords, it consults an environment variable called $PATH that lists which directories the shell should search for programs when it is given a command: 如果 shell 被要求執行一個不符合其程式設計關鍵字的命令時,它會查詢一個名為 $PATH 的環境變數,這個變數列出了當 shell 收到一個 command 時應該在哪些目錄中搜尋 programs:
.
:表示當前目錄..
:表示父目錄cd ./home
和 cd home
效果是一樣的
所以
cd missing
和 cd ./missing
環境變數例如 PATH、HOME、MAIL、SHELL 等等,都是很重要的 HOME:cd ~ 去到自己的家目錄 SHELL:目前這個環境使用的 SHELL 程式 PATH:執行檔搜尋的路徑~目錄與目錄中間以冒號(:)分隔, 由於檔案的搜尋是依序由 PATH 的變數內的目錄來查詢,所以,目錄的順序也是重要的。
export 指令
如果希望將自訂變數變成環境變數的話,讓該變數內容繼續的在子程序中使用,就執行
如果僅寫 export 而沒有接變數時,此時會把所有的『環境變數』都秀出來
echo 指令
source 指令:讀入環境設定檔的指令
重新讀取修改後的設定檔
設定 EV 範例:Configure the ANDROID_SDK_ROOT environment variable
STEP1 Add the following lines to your $HOME/.bash_profile or $HOME/.bashrc (if you are using zsh then ~/.zprofile or ~/.zshrc) config file:
STEP2 Type source $HOME/.bash_profile for bash or source $HOME/.zprofile to load the config into your current shell. STEP3 Verify that ANDROID_SDK_ROOT has been set by running echo $ANDROID_SDK_ROOT and the appropriate directories have been added to your path by running echo $PATH.
結論:簡單的整理一下設定環境變數時會用到的 export, source, echo,也引用一句話~在 Linux 的環境下,如果你不懂 bash 是什麼,那麼其他的東西就不用學了
參考文章:
認識與學習BASH Linux环境变量配置全攻略