“源”命令 [英] "source" command on mac
问题描述
我在Mac OS上有一个 .bashrc
文件,其中我设置了一些别名。在我使它和我键入 source .bashrc
,别名工作完美。但是,如果打开另一个shell,我的快捷命令将无法识别,我需要再次执行 source .bashrc
。
终端和iTerm 2默认打开新shell作为登录shell。当Bash作为登录shell打开时,它读取〜/ .bash_profile
,但不是〜/ .bashrc
p>
请参阅 https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html :
作为交互式登录shell或与--login
调用当Bash作为交互式登录shell或作为非交互式shell调用时 - -login选项,它首先从文件/ etc / profile读取并执行命令(如果该文件存在)。读取该文件后,它按顺序查找〜/ .bash_profile,〜/ .bash_login和〜/ .profile,并从第一个存在并可读的命令读取和执行命令。
因此,可以使用--noprofile选项。
$ b
- 使用
〜/ .bash_profile
而不是〜/ .bashrc
。 - 使用
〜/ .bashrc
,但源〜/ .bashrc
从〜/ .bash_profile
。 - 告诉终端应用程序打开新shell作为非登录shell。
我做了最后两个。例如 tmux
,Emacs中的shell模式会将新shell打开为非登录shell。我仍然从〜/ .bashrc
源〜/ .bash_profile
,因为Bash当我ssh我的电脑。
I have a .bashrc
file on Mac OS in which I set some aliases. After I make it and I type source .bashrc
, the aliases work perfectly. However if open another shell, my shortcut command will not be recognized and I need to do source .bashrc
again. How can it make it once and for all?
Terminal and iTerm 2 open new shells as login shells by default. When Bash is opened as a login shell, it reads ~/.bash_profile
but not ~/.bashrc
.
See https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html:
Invoked as an interactive login shell, or with --login
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.
So either:
- Use
~/.bash_profile
instead of~/.bashrc
. - Use
~/.bashrc
but source~/.bashrc
from~/.bash_profile
. - Tell your terminal application to open new shells as non-login shells.
I have done the last two. For example tmux
and the shell mode in Emacs open new shells as non-login shells. I still source ~/.bashrc
from ~/.bash_profile
because Bash is opened as a login shell when I ssh to my computer.
这篇关于“源”命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!