一、环境配置文件的重要性

  Bash在启动时直接读取这些配置文件,以规划好bash的操作环境。

  即使注销bash,我们的设置仍然保存。

二、login shell

  通过完整的登录流程取得的bash,称为login shell。

  譬如,我们由tty1~tty6登录,需要输入用户的账号与密码,此时取得的bash就称为“login shell”。

三、non-login shell

  不需要通过重复登录取得的bash,成为non-login shell。

  譬如,我们以X Window登录Linux后,再以X的图形界面启动终端,此时得到的bash并没有需要再次输入账号密码,故此bash就是“non-login shell”。

四、环境配置文件概述

  配置文件分为全体系统的配置文件、用户个人偏好配置文件。

  login shell 和 non-login shell 在启动时读取的配置文件并不相同:

  login shell:/etc/profile、~/.bash_profile或~/.bash_login或~/.profile(只存在一个,依不同的distribution而定)

  non-login shell:~/.bashrc

五、/etc/profile(login shell会读)

1. 概述

  • 利用UID来决定很多重要的变量数据

2. 用途

  • 帮所有用户设置整体环境

3. 主要变量

  • PATH:依据UID决定PATH变量要不要含有sbin的系统命令目录
  • MAIL:依据账号设置用户的mailbox到/var/spool/mail/账号名
  • USER:根据用户的账号设置此变量内容
  • HOSTNAME:依据主机的hostname命令决定此变量内容
  • HISTSIZE:历史命令记录条数

4. 调用外部的设置数据

  • /etc/inputrc:此文件内容为bash的热键、[Tab]有没有声音等数据。其实这个文件没有被执行。/etc/profile会主动判断用户有没有自定义输入的按键功能,如果没有的话,/etc/profile就会决定设置“INPUTRC=/etc/inputrc”这个变量。
  • /etc/profile.d/*.sh:这指目录内的众多文件。只要在/etc/profile.d/这个目录内且扩展名为.sh,另外用户能够具有r的权限,那么该文件就会被/etc/profile调用。这个目录下面的文件规定了bash操作接口的颜色、语系、ll与ls命令的命令别名、vi的命令别名、which的命令别名等。如果你需要设置一些共享的命令别名时,可以在这个目录下自行创建扩展名为.sh的文件,并将所需要的数据写入即可。
  • /etc/sysconfig/i18n:这是个决定bash默认使用何种语系的重要配置文件。由/etc/profile.d/lang.sh调用。

六、~/.bash_profile或~/.profile或~/.bash_login(login shell会读)

  Bash在读完了整体环境配置的/etc/profile并借此调用其他配置文件后,接下来就轮到读取用户的个人配置文件。

  在我的Linux系统中是~/.profile文件,其内容为:

# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi # set PATH so it includes user's private bin directories
PATH="$HOME/bin:$HOME/.local/bin:$PATH"

~/.profile

  这个文件内也有设置PATH这个变量(追加设置??)。

  此文件内容还包括读入~/.bashrc的设置。(读入配置文件的方式为“. 配置文件”或“source 配置文件”)

  故我们可以知道,在login shell环境下,最终被读取的配置文件是“~/.bashrc”这个文件。所以我们可以将自己的偏好设置写入该文件。

七、读取配置文件

  方式1:source 配置文件

  方式2:. 配置文件

  作用:将配置文件的内容读进目前的shell环境中,于是配置文件中的设置便会立刻生效。

八、~/.bashrc(non-login shell会读)

  这是non-login shell的环境配置文件。

  当我们取得non-login shell时,仅会读取~/.bashrc这一配置文件。

  以一般用户身份登录,其内容为:

# ~/.bashrc: executed by bash() 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
case $- in
*i*) ;;
*) return;;
esac # don't put duplicate lines or lines starting with space in the history.
# See bash() for more options
HISTCONTROL=ignoreboth # append to the history file, don't overwrite it
shopt -s histappend # for setting history length see HISTSIZE and HISTFILESIZE in bash()
HISTSIZE=
HISTFILESIZE= # check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize # If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar # make less more friendly for non-text input files, see lesspipe()
[ -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|*-256color) 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 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-). (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)}\u@\h:\w\$ '
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 # colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01' # some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF' # Add an "alert" alias for long running commands. Use like so:
# sleep ; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[-]\+\s*//;s/[;&|]\s*alert$//'\'')"' # 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 ! 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

~/.bashrc

    这个文件内规定了一些命令别名、HISTSIZE等。

  最重要的是它会主动调用类似~/.bash_aliases、/usr/share/bash-completion/bash_completion这些文件。

  调用这些文件可以帮我们的bash更完整,譬如会帮助定义以下数据:

  • 依据不同的UID规定umask值;
  • 依据不同的UID规定提示符(就是PS1变量);
  • 调用/etc/profile.d/*.sh的设置。

九、其他影响bash的配置文件

  • /etc/man.config:这个文件的内容规定了使用man的时候man page的路径到哪里去寻找。即规定了执行man的时候该去哪里查看数据的路径设置。对于系统管理员而言,这个文件很重要。
  • ~/.bash_history:这个文件记录历史命令。每次登陆bash后,bash会先读取这个文件,将所有的历史命令读入内存。
  • ~/.bash_logout:这个文件记录了当我注销bash后系统再帮我做完什么操作后才离开。

十、终端机的环境设置

  设置在tty1~tty6这六个命令行界面的终端机的环境。

  目前使用的Linux distributions都帮我们设置了最棒的用户环境,所以我们可以不用设置。

  命令:stty、set

学习bash——环境配置的更多相关文章

  1. 深度学习主机环境配置: Ubuntu16.04+GeForce GTX 1080+TensorFlow

    接上文<深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0>,我们继续来安装 TensorFlow,使其支持GeForce GTX 1080显卡 ...

  2. 深度学习主机环境配置: Ubuntu16.04 + GeForce GTX 1070 + CUDA8.0 + cuDNN5.1 + TensorFlow

    深度学习主机环境配置: Ubuntu16.04 + GeForce GTX 1070 + CUDA8.0 + cuDNN5.1 + TensorFlow 最近在公司做深度学习相关的学习和实验,原来一直 ...

  3. 1 python学习——python环境配置

    1 python学习--python环境配置 要学习python语言,光看书看教程还是不好,得动手去写.当然,不管学习什么编程语言,最佳的方式还在于实践. 要实践,先得有一个Python解释器来解释执 ...

  4. (转)深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0

      深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0 发表于2016年07月15号由52nlp 接上文<深度学习主机攒机小记>,这台GTX10 ...

  5. 第6天【egrep、bash环境配置及脚本、vim编辑器】

    bash环境配置及脚本(02)_recv bash环境配置及脚本(02)_recv bash环境配置文件: 按生效范围划分,存在两类: 全局配置: /etc/profile /etc/bashrc 个 ...

  6. 深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0

    不多说,直接上干货! 深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0

  7. Libgdx游戏学习(1)——环境配置及demo运行

    原文: Libgdx游戏学习(1)--环境配置及demo运行 - Stars-One的杂货小窝 Libgdx游戏是基于Java的一款游戏引擎,可以发布Android,桌面端,Html,IOS等游戏,出 ...

  8. JMeter学习-002-JMeter环境配置

    本节主要介绍 JMeter 本地环境配置(JMeter 版本为 apache-jmeter-2.12),详细配置如下: 一.JDK配置 默认用户本地已经安装且配置好 JDK.若未配置,敬请参阅我的博客 ...

  9. ES6学习之环境配置

    环境配置 一.建立工程目录 新建dist文件夹(用于存放转化的es5文件).新建src文件夹(用于存放es6文件),在该文件夹下建立index.js文件 二.编写index.html 在根目录下新建i ...

随机推荐

  1. 用HTML写伪类选择器,结构伪类选择器,伪元素选择器样式

    html,css lorem乱序铭文 Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, nihil? Lorem ...

  2. iOS开发CGRectGetMidX. CGRectGetMidY.CGRectGetMinY. CGRectGetMaxY. CGRectGetMinX. CGRectGetMaxX的使用

    [iOS开发]iOS开发CGRectGetMidX. CGRectGetMidY.CGRectGetMinY. CGRectGetMaxY. CGRectGetMinX. CGRectGetMaxX的 ...

  3. WebGL学习笔记(2)

    根据上一篇学习笔记,理解WebGL的工作原理及基本调用代码后,可以开始研究3D顶点对象的缩放.旋转.以及对对象进行可交互(鼠标或键盘控制)的旋转或者缩放. 要理解对象的旋转/缩放需要首先学习矩阵的计算 ...

  4. Set linux mq_queue size for user

    设置调整mq_queue的size*num如果大于默认(POSIX message queues),则需要调整系统限制和用户限制,不然在mq_open是会报"Too many open fi ...

  5. 【php练习源码】

    Something is wrong with the XAMPP installation :-( value[$name]=$sex; } public function getInfomatio ...

  6. scala成长之路(4)compaion object——伴生对象的使用

    虽然java一直声称自己是完全面向对象的语言,但一直以来都被很多人所质疑,其中java的静态成员函数就是主要的“罪魁祸首”.由于java中保留了静态方法的调用,导致其编程模式依然有过程式编程的可能,尤 ...

  7. 阅读《大型网站技术架构》,并结合"重大需求征集系统"有感

    今天阅读了<大型网站技术架构:核心原理与案例分析>的第五.六.七章.这三张主要是讲述了一个系统的可用性.伸缩性和可扩展性.而根据文中所讲述的,一个系统的可用性主要是体现在这个系统的系统服务 ...

  8. 5.Python的语言特点

    前言   Python有哪些语言特点?可以列出的特点很多,例如,<Python核心编程>第二版列出了十多条特点.本文的三个特点是笔者学习Python的体会,其他特点有体会之后再写,笔者是这 ...

  9. python2.7练习小例子(二十二)

        22):题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和.     程序分析:请抓住分子与分母的变化规律. #!/usr/bin/pyt ...

  10. 1139: [POI2009]Wie

    1139: [POI2009]Wie https://www.lydsy.com/JudgeOnline/problem.php?id=1139 分析: Dijkstra.状压最短路,dis[i][j ...