找回密码
 注册
首页 ≡≡网络技术≡≡ 服务器 Linux用户目录下三个bash文件的作用(history,logout,pr ...

linux Linux用户目录下三个bash文件的作用(history,logout,profile)

灰儿 2024-2-7 09:09:43
Linux用户目录下三个bash文件的作用(.bash_history,.bash_logout,.bash_profile,.bashrc)

每个用户的根目录下都有四个这样的bash文件,他们是隐藏文件,需要使用-a参数才会显示出来
  1. [yveshe@yveshe ~]$ ll -a | grep bash
  2. -rw-------   1 yveshe yveshe      2910 Jan 12 20:20 .bash_history
  3. -rw-r--r--   1 yveshe yveshe        18 Oct 31 01:07 .bash_logout
  4. -rw-r--r--   1 yveshe yveshe       337 Jan  2 10:53 .bash_profile
  5. -rw-r--r--   1 yveshe yveshe       231 Oct 31 01:07 .bashrc
复制代码


文章目录
① `.bashrc` 介绍
②`.bash_profile` 介绍
③ `.bash_history` 介绍
④ `.bash_logout`介绍
① .bashrc 介绍
用途: 用户定义别名和函数

# .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

示例:

# User specific aliases and functions
PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
LANG=zh_CN.GBK
export PATH LANG

alias rm='rm -i'
alias ls='/bin/ls -F --color=tty --show-control-chars'

例子中定义了路径,语言,命令别名(使用rm删除命令时总是加上-i参数需要用户确认,使用ls命令列出文件列表时加上颜色显示)。
每次修改.bashrc后,使用source ~/.bashrc(或者 . ~/.bashrc)就可以立刻加载修改后的设置,使之生效。

一般会在.bash_profile文件中显式调用.bashrc。登陆linux启动bash时首先会去读取~/.bash_profile文件,这样~/.bashrc也就得到执行了,你的个性化设置也就生效了。

关于环境变量的读取顺序:
用户登录 ->> 加载~/.bash_profile --> bash_profile中配置了首先是使~/.bashrc生效

②.bash_profile 介绍
# .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


# 用户自定义一些环境变量,以及添加查询路径到PATH中
export JAVA_HOME=/home/yveshe/jdk1.8
export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH

示例代码中为用户添加了2个环境变量(JAVA_HOME和CLASSPATH),并修改了已有环境变量PATH的值.(PATH的查找是从前开始查找,找到就返回)
CLASSPATH环境变量的值是在JAVA运行时查找加载类的默认classpath.

③ .bash_history 介绍
用途: 保存了当前用户使用过的历史命令,方便查找
详情参考: https://blog.csdn.net/caolaosanahnu/article/details/7601074

④ .bash_logout介绍
用途: 用户登出时执行的命令

# ~/.bash_logout

# 在当前用户登出时,打印出Logout 和当前的时间
echo "Logout, `date`"

例子:
如果需要每个用户登出时都清除输入的命令历史记录,可以在/etc/skel/.bash_logout文件中添加下面这行rm -f $HOME/.bash_history 。这样,当用户每次注销时,.bash_history文件都会被删除.
————————————————                       
原文链接:https://blog.csdn.net/u011479200/article/details/86501366

您需要登录后才可以回帖 登录 | 注册
学习中心
站长自定义文字内容,利用碎片时间,随时随地获取优质内容。
Q设计语言 了解更多
Q Design 提供商家设计所需的指导与资源,帮商家快速完成产品设计、降低生产成本。
学习中心
站长自定义文字内容,利用碎片时间,随时随地获取优质内容。
Q设计语言 了解更多
Q Design 提供商家设计所需的指导与资源,帮商家快速完成产品设计、降低生产成本。