16 liunx shell基础 环境变量配置文件/etc/profile、~/.bash_profile、~/.bashrc、/etc/bashrc详解及执行顺序 ~/.bash source命令

在这里插入图片描述

环境变量配置文件

环境变量配置文件:顾名思义就是存放环境变量的文件。
环境变量配置文件中的环境变量,系统任何时候启动都会生效。

source命令

source命令通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录。

语法格式:source [文件]
或 source 和 . 点作用相同
语法格式:. [文件]

常用的环境变量

  • HOME 代表用户的家目录。 cd ~ 到自己的家目录, ~ 是一个环境变量
  • SHELL 告知,该系统支持那种shell脚本, Linux 默认使用 /bin/bash 的啦!
  • HISTSIZE history命令记录多少条命令
  • MAIL 使用 mail 命令收信时,系统读取的邮件信箱文件 (mailbox)。
  • PATH 运行文件搜寻的路径;目录与目录中间以冒号(:)分隔, 文件搜寻依次按 PATH 变量中配置的目录来查询,所以,目录的顺序也是重要。
  • RANDOM『随机随机数』的变量; 大多数的 distributions 都会有随机数生成器,那就是 /dev/random 这个文件。 $RANDOM) 来随机取得随机数值 0~32767 之间的值。

环境变量配置文件

针对所有用户的配置文件:
/etc/profile
/etc/profile.d/*.sh
/etc/bashrc

针对当前登录用户的配置文件:
~/.bash_profile
~/.bashrc

linux中使用[.]点开头命名的文件为隐藏文件。ls -a 或 ll -a 查看

环境变量配置文件作用

配置文件的执行顺序

在这里插入图片描述

1. /etc/profile 作用:定义如下环境变量

  • USER变量
  • LOGNAME变量
  • MAIL变量
  • PATH变量
  • HOSTNAME变量
  • HISTSIZE变量
  • umask变量
  • 调用/etc/profile.d/*.sh文件
pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}

# 配置USER LOGNAME MAIL 环境变量
if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`id -u`
        UID=`id -ru`
    fi
    USER="`id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi

# 配置PATH 环境变量  调用 pathmunge   $EUID=0 代表root用户
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge

2. ~/.bash_profile 作用

执行 . ~/.bashrc
修改 PATH

# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH

3. ~/.bashrc 作用

执行 . /etc/bashrc
User specific aliases and functions: 定义当前用户定义的别名

# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

4. /etc/bashrc 作用

[ “$PS1” = “\s-\v\$ " ] && PS1=”[\u@\h \W]\$ " 定义默认登录提示符


# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

# are we an interactive shell?
if [ "$PS1" ]; then
  if [ -z "$PROMPT_COMMAND" ]; then
    case $TERM in
    xterm*|vte*)
      if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
      elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then
          PROMPT_COMMAND="__vte_prompt_command"
      else
          PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
      fi
      ;;
    screen*)
      if [ -e /etc/sysconfig/bash-prompt-screen ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
      else
          PROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
      fi
      ;;
    *)
      [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
      ;;
    esac
  fi
  # Turn on parallel history
  shopt -s histappend
  history -a

  # Turn on checkwinsize
  shopt -s checkwinsize
  [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
  # You might want to have e.g. tty in prompt (e.g. more virtual machines)
  # and console windows
  # If you want to do so, just add e.g.
  # if [ "$PS1" ]; then
  #   PS1="[\u@\h:\l \W]\\$ "
  # fi
  # to your custom modification shell script in /etc/profile.d/ directory
fi

if ! shopt -q login_shell ; then # We're not a login shell
    # Need to redefine pathmunge, it get's undefined at the end of /etc/profile
    pathmunge () {
        case ":${PATH}:" in
            *:"$1":*)
                ;;
            *)
                if [ "$2" = "after" ] ; then
                    PATH=$PATH:$1
                else
                    PATH=$1:$PATH
                fi
        esac
    }

    # By default, we want umask to get set. This sets it for non-login shell.
    # Current threshold for system reserved uid/gids is 200
    # You could check uidgid reservation validity in
    # /usr/share/doc/setup-*/uidgid file
    if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
       umask 002
    else
       umask 022
    fi

    SHELL=/bin/bash
    # Only display echos from profile.d scripts if we are no login shell
    # and interactive - otherwise just process them to set envvars
    for i in /etc/profile.d/*.sh; do
        if [ -r "$i" ]; then
            if [ "$PS1" ]; then
                . "$i"
            else
                . "$i" >/dev/null
            fi
        fi
    done

    unset i
    unset -f pathmunge
fi
# vim:ts=4:sw=4

该配置文件中重新定义了PATH、umask 等的值;
不是重复定义,定义了不使用账号密码登录情况的下PATH、umask 等的值。 We’re not a login shell 文档中已经注释了。

其他配置

注销时生效的环境变量配置文件:~/.bash_logout
历史命令的配置文件:~/.bash_histtory

登录信息

  • /etc/issue 本地登陆显示的信息,本地登录前
  • /etc/issue.net ==网络登陆(远程登录)==显示的信息,登录后显示,需要由sshd配置
  • /etc/motd 通告信息本地登录远程登录都显示,如计划关机时间的警告等,登陆后的提示信息

/etc/issue转义符如下

转义符作用
\d本地端时间的日期
\s系统的名称
\l显示登录终端号 ;显示当前tty的名字即第几个tty
\m显示硬体的架构 (i386/i486/i586/i686…)
\n显示主机名
\o显示 domain name 域名
\r当前系统内核版本 (相当于 uname -r)
\t显示本地端时间的时间
\u当前用户序列号
\v当前系统的版本
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EngineerForSoul

你的鼓励是我孜孜不倦的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值