“源”命令 [英] "source" command on mac

查看:204
本文介绍了“源”命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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


  1. 使用〜/ .bash_profile 而不是〜/ .bashrc

  2. 使用〜/ .bashrc ,但源〜/ .bashrc 〜/ .bash_profile

  3. 告诉终端应用程序打开新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:

  1. Use ~/.bash_profile instead of ~/.bashrc.
  2. Use ~/.bashrc but source ~/.bashrc from ~/.bash_profile.
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆