在linux中,可以利用unset命令刪除環境變量,該命令用于刪除變量或者函數,語法為“unset 變量名”;在shell子進程中使用unset命令刪除環境變量時,只會影響當前子進程的可見性,不會影響父shell進程。
本教程操作環境:linux7.3系統、Dell G3電腦。
linux怎么刪除環境變量
1.?刪除全局環境變量
在?/etc/profile?文件中添加的?test_val=my_test_dog?變量是一個全局環境變量,可以使用?unset?命令刪除這個全局環境變量,命令如下:
echo?$test_val unset?test_val echo?$test_val
執行結果如下圖:
Tips:因為?test_val?變量的值在文件?/etc/profile?中,被?unset?命令刪除之后,可以重新使用?source?/etc/profile?命令更新權限,重新獲取到?test_val?變量的值。
2.?在?shell?子進程中刪除環境變量
在?shell?子進程中可以使用?unset?命令刪除指定的環境變量名,命令如下:
new_val="5axxw" export?new_val bash echo?$new_val unset?new_val echo?$new_val exit echo?$new_val
執行結果如下圖:
3.?小結
本小節主要介紹如何使用?unset?命令刪除環境變量,需要注意的是在?shell?子進程中可見的全局環境變量,使用?unset?命令刪除只影響當前子進程的可見性,不會影響到父?shell?進程。另外一般在使用環境變量時,變量名前面要帶上?$,若是對變量操作,則不需要前面的?$?。
相關推薦:《Linux視頻教程》
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END
喜歡就支持一下吧
相關推薦