工做中常常遇见环境变量加载问题,归根结底就是配置文件的加载问题。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/