.bash_profile VS .bashrc

简介:

以前常用Linux,最近在Mac的terminal上配置git命令自动补全,发现将配置写入~/.bashrc中不起作用,网上查到别人是将配置写入~/.bash_profile.其实自己以前一直也没有好好弄明白这两个文件的区别。今天查了一下,整理在笔记中:

在Linux,Unix, 和Mac OS X中,都可以通过在home目录中的 .bashrc和 .bash_profile来配置bash. 如果home目录中没有,这两个文件都是可以手动创建的。但是为什么会有两个不同的配置文件呢?他们之间的有什么区别?


详解:

在terminal里运行一下$man bash, 通过关键字查找,可以看到一些提示信息:

       The  following  paragraphs describe how bash executes its startup files.  If any of the files exist but cannot be read,bash reports an

       error.  Tildes are expanded in file names as described below underTildeExpansion in theEXPANSION section.


       When bash is invoked as an interactive login shell, or as a non-interactive shell with the--login option, it first reads and  executes

       commands  from  the  file/etc/profile, if that file exists.  After reading that file, it looks for~/.bash_profile,~/.bash_login, and

       ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.  The--noprofile option  may

       be used when the shell is started to inhibit this behavior.


       When a login shell exits,bash reads and executes commands from the file~/.bash_logout, if it exists.


       When  an  interactive shell that is not a login shell is started,bash reads and executes commands from~/.bashrc, if that file exists.

       This may be inhibited by using the--norc option.  The--rcfilefile option will forcebash to read  and  execute  commands  from file

       instead of ~/.bashrc.


   ~/.bash_profile

              The personal initialization file, executed for login shells

   ~/.bashrc

              The individual per-interactive-shell startup file

可以明显的看到,.bash_profile 是登录的shell的个人配置初始化文件。.bashrc是非登录shell的交互启动文件,关键在于登录与非登录。


当我们登录(login)时,系统会要求输入用户名和密码,通过ssh命令远程登录时也是要求每次输入用户名和密码,除非配置ssh key(也是登录认证)。这时候.bash_profile会在初始化命令之前被执行,用来配置shell,可以通过users命令查看当前登录的用户。

如果已经登录到电脑中,再重新在Gnome或者KDE开一个终端窗口(比如xterm). 这时.bashrc在弹出命令提示窗口之前被执行。在终端中运行/bin/bash, 将会执行.bashrc

我在我的Mac terminal的home目录下同时创建了.bashrc 文件和 .bash_profile文件,并且在这两个文件的末尾行分别加了一句测试语句:

~/.bashrc:

echo -e "\n~/.bashrc is executed!"

~/.bash_profile:

echo -e "\n~/.bash_profile is executed!"

下面是运行结果:

重新打开一个terminal,执行.bash_profile, 在terminal中运行/bin/bash, 执行.bashrc


.bash_profile 只在登录时执行,.bashrc在每次创建新窗口时都会执行。Mac中默认是执行.bash_profile, 即登录shell。


如果让.bashrc在登录shell时也运行,这样会让两个文件中的设置同时生效,而不用配置两次,可以在~/.bash_profile中加入一下语句:   

if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: .bash_profile和.bashrc是两个文件名,用于定制Bash shell的行为和环境。它们通常用于Unix-like系统,如LinuxMac OS。 .bash_profile是用户的个人配置文件,它位于用户的主目录下。当用户登录时,Bash shell会先读取.bash_profile文件,并执行其中的命令。用户可以在这个文件中设置环境变量、别名、函数等个性化配置。例如,用户可以在.bash_profile中添加export命令来设置环境变量,以便在每次登录时自动定义一些变量。 .bashrcBash shell的运行配置文件,它位于用户的主目录下或者全局配置文件夹中。当用户在已经登录的终端窗口打开新的Bash shell时,Bash shell会读取并执行.bashrc文件中的命令。用户可以在这个文件中定义自己喜欢的别名、函数、命令行选项等配置。例如,用户可以在.bashrc中定义一个别名,使得输入这个别名时会执行一条长而常用的命令。 .bash_profile和.bashrc的区别在于执行的时机。.bash_profile只在用户登录时执行一次,而.bashrc在每次打开新的Bash shell时执行。这样设计的目的是为了避免在每次打开新的终端窗口时都执行一次较慢的命令。 总之,.bash_profile和.bashrc是用于配置Bash shell行为和环境的文件,它们的作用是定制化用户的命令行体验,使得用户可以根据自己的需求进行个性化配置。 ### 回答2: .bash_profile 和 .bashrc 都是与 Bash(Unix 和 Linux 操作系统的默认 Shell)相关的配置文件。虽然它们的作用有些重叠,但也有一些区别。 .bash_profile 是用户登录时 Bash 首先加载的文件。它主要用于个人用户特定的登录配置。在这个文件中,我们可以定义一些用户特定的环境变量、别名和函数等配置。这些配置会在用户登录时生效,但在新的终端窗口或会话中并不会重新加载。 .bashrc 是每个新的终端窗口或会话启动时加载的文件。它主要用于每个 Shell 实例通用的配置。在这个文件中,可以定义一些通用的环境变量、别名和函数等配置。这些配置会在每次新的终端窗口或会话启动时生效。 .bash_profile 的加载是在登录时进行的,而 .bashrc 的加载是在每个新的终端窗口或会话启动时进行的。所以,.bash_profile 主要用于用户登录时的个人配置,而 .bashrc 则用于每个新的终端窗口或会话的通用配置。 当我们需要配置一些只与用户登录有关的设置时,我们应该将这些设置放在 .bash_profile 中。而当我们需要配置一些与 Shell 实例无关的通用设置时,我们应该将这些设置放在 .bashrc 中,以保证每个终端窗口或会话启动时都能生效。 总结起来,.bash_profile 是用户登录时 Bash 加载的个人配置文件,而 .bashrc 是每个新的终端窗口或会话启动时加载的通用配置文件。通过合理使用这两个文件,我们可以根据需要定制自己的 Shell 环境。 ### 回答3: .bash_profile和.bashrc是两个与Shell相关的常用配置文件。 .bash_profile是用户登录到系统时,Shell会首先加载和执行的配置文件。它通常用于设置用户的个性化环境变量、加载自定义函数和别名等操作。比如,可以在.bash_profile中定义PATH变量,指定用户首选的路径顺序,还可以加载个人的shell函数、别名和自定义命令等。 .bashrc是每次打开新的终端窗口时都会加载和执行的配置文件。它通常用于设置用户习惯的Shell环境,比如定义用户喜欢的别名、快捷键、自定义提示符等。.bashrc通常会被.bash_profile首先加载,所以在.bash_profile中可以通过source命令引入.bashrc,以确保两个配置文件的设置都能生效。 这两个配置文件的区别在于加载的时机和作用范围。.bash_profile只在用户登录时加载一次,因此它适合于一些只需要设置一次的环境变量和加载自定义函数等操作。而.bashrc在每次打开新的终端窗口时都会加载,所以适合用于定制Shell环境和设置一些与特定终端会话相关的配置。 当用户登录到系统时,Shell会首先加载.bash_profile文件,然后再加载.bashrc文件,以确保用户个性化设置和习惯的Shell环境能够正确生效。通过正确配置这两个文件,用户可以实现自定义的Shell环境,提高工作效率和使用舒适度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值