gracekrcx / weekly-notes

4 stars 0 forks source link

(筆記)Operating System 裡的 environment variable #128

Open gracekrcx opened 2 years ago

gracekrcx commented 2 years ago

環境變數例如 PATH、HOME、MAIL、SHELL 等等,都是很重要的 HOME:cd ~ 去到自己的家目錄 SHELL:目前這個環境使用的 SHELL 程式 PATH:執行檔搜尋的路徑~目錄與目錄中間以冒號(:)分隔, 由於檔案的搜尋是依序由 PATH 的變數內的目錄來查詢,所以,目錄的順序也是重要的。

// 列出所有環境變數
$ env

export 指令

如果希望將自訂變數變成環境變數的話,讓該變數內容繼續的在子程序中使用,就執行

$ export 變數名稱

如果僅寫 export 而沒有接變數時,此時會把所有的『環境變數』都秀出來

$ export

echo 指令

// 變數在被取用時,前面必須要加上錢字號($)
$ echo $HOME  
$ echo $PATH

source 指令:讀入環境設定檔的指令

重新讀取修改後的設定檔

// 將家目錄的 ~/.zshrc 的設定讀入目前的 shell 環境中
// 底下這兩個指令是一樣的
$ source ~/.zshrc
$ . ~/.zshrc

設定 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:

export ANDROID_SDK_ROOT=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_SDK_ROOT/emulator
export PATH=$PATH:$ANDROID_SDK_ROOT/platform-tools

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环境变量配置全攻略

gracekrcx commented 2 years ago

什麼是『子程序』呢?就是說,在我目前這個 shell 的情況下,去啟用另一個新的 shell ,新的那個 shell 就是子程序啦!在一般的狀態下,父程序的自訂變數是無法在子程序內使用的。但是透過 export 將變數變成環境變數後,就能夠在子程序底下應用了!

gracekrcx commented 2 years ago

環境變數

gracekrcx commented 2 years ago

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
gracekrcx commented 2 years ago

Heroku dynamic port for your app to bind to.

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`Our app is running on port ${ PORT }`);
});

Why is my Node.js app crashing with an R10 error?

gracekrcx commented 10 months ago

bash shell commond line

gracekrcx commented 1 week ago

Course overview + the shell

命令執行過程:

$PATH 環境變數:

gracekrcx commented 1 week ago

.:表示當前目錄

..:表示父目錄

cd ./homecd home 效果是一樣的 所以 cd missingcd ./missing