1.Ubuntu 理解 bashrc 和 profile

bashrc和profile的区别

bashrc 看名字就知道, 是专门用来给 bash 做初始化的,比如用来初始化 bash 的设置, bash 的代码补全, bash 的别名, bash 的颜色. 以此类推也就还会有 shrc, zshrc 这样的文件存在了, 只是 bash 目前是最常用的shell,bashrc中rc是run commands或run configure的缩写。

profile 看名字就能了解大概了, profile 是某个用户唯一的用来设置环境变量的地方, 因为用户可能有多个 shell 比如 bash, sh, zsh 之类的, 但像环境变量这种其实只需要在统一的一个地方初始化就可以了, 而这就是 profile.

可以通过 finger [USERNAME] 命令来查看自己所用的shell.

/etc/profile
/etc/bashrc <--Fedora:Redhat:Centos
/etc/bash.bashrc <--Debian:Ubuntu

~/.bashrc
~/.profile

什么时候运行这些文件

需要先了解Shell 的 login(登入) 和 interactive(交互式) 模式Shell 的模式
“interactive shell” 是和用户交互的shell, 会有一个叫 PS1 的环境变量。
“no-login” 是指 打开ubuntu打开默认终端 或者 终端下直接输入bash切换shell 或者 bash -c “CMD”执行命令而启动的 shell

“interactive login shell” :用户输入账号密码登入,此时shell会去自动执行 

/etc/profile
 ~/.profile

“interactive no-login shell”:此时shell会去自动执行

/etc/bash.bashrc
~/.bashrc

举例说明:
1.SSH login, sudo su - [USER]
SSH 登入和 su -是典型的 interactive login shell, 会有 PS1 变量, 并且会执行

/etc/profile
~/.profile

2.在命令提示符状态下输入 bash 或者 ubuntu 默认设置下打开终端
是典型的 interactive no-login shell, 会有 PS1 变量, 会执行

/etc/bash.bashrc
~/.bashrc

3.通过 bash -c “CMD” 或者 bash BASHFILE 命令执行的 shell
这些命令什么都不会执行, 也就是设置 PS1 变量, 不执行任何 RC 文件
4.通过 “ssh server CMD” 执行的命令 或 通过程序执行远程的命令
这是最特殊的一种模式, 理论上应该既是 非交互 也是 非登入的, 但是实际上他不会设置 PS1, 但是还会执行

/etc/bash.bashrc
~/.bashrc

这里还有一点值得注意的是 /etc/bashrc 任何情况下都不会执行.

cat /etc/profile

发现其source /etc/bash.bashrc 文件和加载/etc/profile.d中的每个文件

[root@Orz 17:02 ~]# cat /etc/profile
# /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

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi
[root@Orz 19:19 ~]# la /etc/profile.d
01-locale-fix.sh           bash_completion.sh     im-config_wayland.sh
Z97-byobu.sh               cedilla-portuguese.sh  update-motd.sh
Z99-cloud-locale-test.sh   env.sh                 vte-2.91.sh
Z99-cloudinit-warnings.sh  gawk.csh               vte.csh
apps-bin-path.sh           gawk.sh                xdg_dirs_desktop_session.sh

cat ~/.profile

 发现其source ~/.bashrc 文件

[root@Orz 18:47 ~]# cat ~/.profile
# ~/.profile: executed by Bourne-compatible login shells.

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

mesg n 2> /dev/null || true

cat /etc/bash.bashrc

这里面主要是设置了PS1变量,全局通用的alias 

[root@Orz 19:02 ~]# cat /etc/bash.bashrc
alias runssh="sudo service ssh start"
alias cdcpp="cd /mnt/d/linux/cpp"
alias runcpp="/mnt/d/linux/cpp/run.sh"
alias cdc="cd /mnt/d/linux/c"
alias runc="/mnt/d/linux/c/run.sh"
# System-wide .bashrc file for interactive bash(1) shells.

# To enable the settings / commands in this file for login shells as well,
# this file has to be sourced in /etc/profile.

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, overwrite the one in /etc/profile)
# but only if not SUDOing and have SUDO_PS1 set; then assume smart user.
if ! [ -n "${SUDO_USER}" -a -n "${SUDO_PS1}" ]; then
  PS1='${debian_chroot:+($debian_chroot)}\[\e[1;36m\][\u@\h \A \w]\$\[\e[0m\]\$ '
fi

# Commented out, don't overwrite xterm -T "title" -n "icontitle" by default.
# If this is an xterm set the title to user@host:dir
#case "$TERM" in
#xterm*|rxvt*)
#    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
#    ;;
#*)
#    ;;
#esac

# enable bash completion in interactive shells
#if ! shopt -oq posix; then
#  if [ -f /usr/share/bash-completion/bash_completion ]; then
#    . /usr/share/bash-completion/bash_completion
#  elif [ -f /etc/bash_completion ]; then
#    . /etc/bash_completion
#  fi
#fi

# sudo hint
if [ ! -e "$HOME/.sudo_as_admin_successful" ] && [ ! -e "$HOME/.hushlogin" ] ; then
    case " $(groups) " in *\ admin\ *|*\ sudo\ *)
    if [ -x /usr/bin/sudo ]; then
	cat <<-EOF
	To run a command as administrator (user "root"), use "sudo <command>".
	See "man sudo_root" for details.
	
	EOF
    fi
    esac
fi

# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
	function command_not_found_handle {
	        # check because c-n-f could've been removed in the meantime
                if [ -x /usr/lib/command-not-found ]; then
		   /usr/lib/command-not-found -- "$1"
                   return $?
                elif [ -x /usr/share/command-not-found/command-not-found ]; then
		   /usr/share/command-not-found/command-not-found -- "$1"
                   return $?
		else
		   printf "%s: command not found\n" "$1" >&2
		   return 127
		fi
	}
fi

 cat ~/.bashrc

一些alias和环境变量path 

[root@Orz 19:02 ~]# cat ~/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\[\e[1;36m\][\u@\h \A \w]\$\[\e[0m\] '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
#    . /etc/bash_completion
#fi
PATH=$(/usr/bin/printenv PATH | /usr/bin/perl -ne 'print join(":", grep { !/\/mnt\/[a-z]/ } split(/:/));')

 为什么要搞得这么麻烦,因为linux是的多用户登录系统,不同的用户登录系统,需要加载的配置也不一样,这就是为什么先加载/etc/profile引出/etc/bash.bashrc,加载 ~/.profile引出~/.bashrc,前者是系统的,后者是用户个性化的


理解 bashrc 和 profile - { wido.me }https://wido.me/sunteya/understand-bashrc-and-profile/

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值