工做中常常遇見環境變量加載問題,歸根結底就是配置文件的加載問題。shell
通常會有四種模式:交互式登錄、非交互式登錄、交互式非登錄、非交互非登錄。bash
交互式和非交互式對環境變量的加載:ssh
+----------------+--------+-----------+---------------+ | | login |interactive|non-interactive| | | |non-login |non-login | +----------------+--------+-----------+---------------+ |/etc/profile | A | | | +----------------+--------+-----------+---------------+ |/etc/bash.bashrc| | A | | +----------------+--------+-----------+---------------+ |~/.bashrc | | B | | +----------------+--------+-----------+---------------+ |~/.bash_profile | B1 | | | +----------------+--------+-----------+---------------+ |~/.bash_login | B2 | | | +----------------+--------+-----------+---------------+ |~/.profile | B3 | | | +----------------+--------+-----------+---------------+ |BASH_ENV | | | A | +----------------+--------+-----------+---------------+
bash的每種模式會讀取其所在列的內容,首先執行A,而後是B,C。而B1,B2和B3表示只會執行第一個存在的文件。spa
交互式登錄:簡單示例:code
a.用戶直接登錄到機器得到的第一個shellblog
b.用戶使用 ssh user@remote 得到的shell進程
非交互式登錄:ip
bash -l script.shrem
交互式非登錄:terminal
在已有的shell中運行bash,此時不須要登錄
非交互式非登錄:
a. bash script.sh
b. ssh user@remote command
爲了更好的理清這幾種模式,下面咱們對一些典型的啓動方式各屬於什麼模式進行一個總結:
bash
:non-login + interactivebash script.sh
:non-login + non-interactive#!/usr/bin/env bash
的可執行文件,如./executable
:non-login + non-interactivessh user@remote script.sh
:non-login + non-interactivessh user@remote -t 'echo $PWD'
:non-login + interactive
參考連接:http://feihu.me/blog/2014/env-problem-when-ssh-executing-command-on-remote/