登录和登出一个Linux终端,系统都将会根据系统或者用户配置文件规定的事情。
环境变量都有哪些?shell配置文件有:
文件含义/bin/bashbash可执行程序/etc/profile系统执行的,登陆shell执行的文件/etc/bash.bashrc系统执行的,每个shell都执行的文件/etc/bash.bash.logout系统执行的,登出shell都会执行的清理文件~/.bash_profile个人执行的,登陆shell执行的文件~/.bashrc个人执行的,每个shell都会执行的文件~/.bash_logout个人执行的,登出shell都会执行的清理文件~/.inputrc文字命令的初始化文件对于个人使用,只需要明白~/.bash_profile
是个人的配置文件,当然由于历史原因,配置也可能存在于~/.bash_login
和~/.profile
两个等效文件中。
那么可以怎么查看环境变量?env
推荐或export
。
使用env
查看当前环境变量。export
也可以查看环境变量,不过其形式都是declare -x
声明的形式。
/etc/profile
是系统的整体配置,一般不作修改。Ubuntu16.04下内容如下:
# /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
脚本主要做了两件事情:
. /etc/bash.bashrc
:重读bash.bashrc
文件内容- 执行
/etc/profile.d
下的所有.sh
脚本
bash.bashrc
完成了一些提示性语言,和终端外观控制等,其内容如下:
# 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)
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
# 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 &2
return 127
fi
}
fi
第二步 用户配置读取
按一定顺序依次读取不同习惯用户配置文件~/.bash_profile
or ~./bash_login
or ~/.profile
。他们都是家目录下的隐藏文件,对ubuntu而言,只有默认的~/.profile。以这个文件说明用户配置都有哪些。
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
# 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"
- 刷新形式读取
~/.bashrc
文件内容 - 设置
PATH
变量私有用户命令($HOME/bin
)
~/.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
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) 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(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
# 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(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|*-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 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)}\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'
alias rd='rm -rf'
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\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
主要完成以下内容:
- 别名。简单的别名
alias ls='ls --color=auto'
和别名配置文件调用. ~/.bash_aliases
- 补全。如,
. /usr/share/bash-completion/bash_completion
. /etc/bash_completion
在文件中所有的补全脚本程序都会被执行,如git-prompt
,几乎所有的程序的配置文件(或文件夹)都会放置在~/.*
下,像是vim
git
thunder
cmake
vscode
等。像是git补全程序就放在. /etc/bash_completion
中由文件~/.bashrc
调用。
用户安装的软件配置文件一般会被放在HOME目录下的与软件名有关的隐藏文件中,登录时将会读取这些配置并输出至环境变量中。如VIM和GIT设置:
我们常用的vim行号开启就是在~/.vimrc中
写入。
- ~/.vimrc
- ~/.gitconfig
通常配置文件需要用户启动时才生效,因为此时已经登录,所以只能通过重新登陆或者source
or .
来实现。
- VIM常用配置
set number
set tabstop=4 # 一个TAB多少空格
set shiftwidth=4 # 级间缩进空格数
syntax on
set tabstop=4
set softtabstop=4
set shiftwidth=4
set autoindent
set nu
set nocompatible
filetype plugin indent on 75 hi Comment ctermfg =blue ##解决蓝色注释看不清的问题
- git别名选项别名配置
[user]
name = yourname
email = yourname@email.com
[color]
ui = true
diff = true
status = true
branch = true
interactive = true
[alias] //配置别名
st = status
ss = status -s
ci = commit
ca = commit --amend
co = checkout
ll = log --graph --color --format=format:'%C(bold)%h%C(reset) -%C(bold)%d%C(reset) %C(white)%s%C(reset) %C(bold red)- %an' --abbrev-commit
ft = fetch
pl = pull --rebase
br = branch
cp = cherry-pick
mg = merge
rb = rebase
dci = dcommit
sbu = submodule update
git别名是选项别名,不是我们环境变量中的别名,这要注意。
- 添加环境变量 对于个人用户只需要在
~/.bashrc
中最后一行添加export PATH=$PATH:/usr/local/arm/XXX
matlab添加到环境变量方便直接在终端敲入matlab即可执行,添加到以下语句: export/usr/local/MATLAB/R2018a/bin
添加命令所在的目录即可,一般是XXX/bin
- 命令别名 为了简化命令操作,常常会用到别名。别名可以直接在终端输入
alias 简化命令='长或复杂命令'”
,要想一直生效把这一段命令放在.bashrc
或者等效配置文件中即可。