profile、environment和bashrc的区别

Bash运行相关的配置文件,摘自man pageshell

  • /bin/bash
    The bash executable
  • /etc/profile
    The systemwide initialization file, executed for login shells
  • /etc/bash.bashrc
    The systemwide per-interactive-shell startup file
  • /etc/bash.bash.logout
    The systemwide login shell cleanup file, executed when a login shell exits
  • ~/.bash_profile
    The personal initialization file, executed for login shells
  • ~/.bashrc
    The individual per-interactive-shell startup file
  • ~/.bash_logout
    The individual login shell cleanup file, executed when a login shell exits
  • ~/.inputrc
    Individual readline initialization file

##/etc/profileubuntu

/etc/profile:此文件为系统的每一个用户设置环境信息,当用户第一次登陆时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置.bash

  • profile 使用Bourne shell语法(兼容ksh,csh)等等(zsh?)。
  • profile 中的内容是为全部用户登录作初始化的。

例如ubuntu下/etc/profile的默认内容以下:ide

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "$PS1" ]; then
  if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

# The default umask is now handled by pam_umask.
# See pam_umask(8) and /etc/login.defs.

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

`code

前半段主要是加载/etc/bash.bashrc的内容,后半段则是加载bash的一些初始化脚本,例如bash补全设置。input

##~/.bashrc和/etc/bashrc /etc/bashrc: 为每个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取。it

  • Debian默认没有/etc/bashrc,可是能够新建一个并设置alias。
  • 有一个/etc/bash.bashrc/(命名真的好不科学),它的做用是初始化交互式shell相关,默认是配置PS1变量。
  • ~/.bashrc为每一个用户的bash配置文件,仅针对对应的用户有效。
  • ~/.bashrc可使用bash的专有语法(因此一些fuction定义和alias能够在里面设置)。

/etc/enviroment

/etc/environment是设置整个系统的环境 * 最典型的是ubuntu里的全局path设置io

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
  • Debian之中这个文件是空的,PATH属性在profile设置。
  • 不清楚可否兼容Bourne shell,能够设置各类环境变量。

##执行顺序 /etc/environment -> /etc/profile -> /etc/bashrc -> ~/profile -> ~/.bashrc  后面设置的会覆盖前面的设置。zsh

  • 例如你在四个文件设置了不一样的变量,最后生效的是~/.bashrc的那个 。
  • 位于/etc下的几个文件我的感受实际上起到了一个默认配置的做用。

##bashrc的一点配置原则 对于/etc/bashrc来讲,我的感受应该尽最小配置原则,只有肯定全部用户都用的到的配置才写到里面。事实上Debian干脆默认就没有/etc/bashrc这个文件,每当新建一个用户的时候,都根据从/etc/skel/的模版文件生成一份用户的~/.bashrc和~/.profile。若是你把太多的配置都集中到/etc/bashrc中,某些特殊须要的用户就须要在本身的文件中覆盖这些选项,与上一种方法相比,增长了人工,得不偿失。table

相关文章
相关标签/搜索