Mac OS X .bashrc 不工作

Wil*_*sch 101 bash bashrc macos

.bashrc我的主目录中有一个单行文件:

alias countlines='find . -type f -print0 | xargs -0 cat | wc -l'
Run Code Online (Sandbox Code Playgroud)

但它没有创建别名。为什么会这样?

Azz*_*Azz 129

在 OSX 中,.bash_profile使用 代替.bashrc.

是的,.bash_profile文件应该位于/Users/YourName/
(换句话说,~/.bash_profile

例如, /Users/Aaron/.bash_profile

  • 这不是正确的答案。别名不会被继承,因此,如果您只在 .bash_profile 中定义它们,它们将不会在非登录 shell 中定义(例如,当您在 bash 中运行 bash 时)。 (16认同)
  • 在我的 .bash_profile 中,我只写了一行别名(有点)bashrc -> `source ~/.bashrc` (6认同)

LaC*_*LaC 104

.[bash_]profile并且.bashrc可以在 OS X 和 Linux 上使用。前者在shell为登录shell时加载;后者不是。真正的区别在于,当用户登录图形会话时,Linux 运行一个登录 shell,然后当你打开一个终端应用程序时,这些 shell 是非登录 shell;而 OS X 在图形登录时不会运行 shell,而当您从 Terminal.app 运行 shell 时,那就是登录 shell。

如果你想让你的别名在登录和非登录 shell 中工作(你通常这样做),你应该把它们放在 .bashrc源 .bashrc 在你的 .bash_profile 中,像这样的一行:

[ -r ~/.bashrc ] && source ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

这适用于任何使用 bash 的系统。

  • +1 需要注意的是,.bashrc 中的所有内容都将针对子 shell(以及 subsub-、subsubsub- 等)再次运行,因此例如 `PATH=$PATH:/my/private/binaries` 将导致 PATH 膨胀。有关解决方法,请参阅 [this](http://superuser.com/questions/39751/add-directory-to-path-if-its-not-already-there/39995#39995)。 (17认同)
  • @dinosaur:“-r”检查文件是否可读。 (3认同)
  • 真的。由于导出的实例变量是继承的,我只是将它们设置在 `.profile` 而不是 `.bashrc` 中。 (2认同)

小智 17

由于MacOS Catalina zsh是默认 shell。在此操作系统上将别名添加到~/.zshrc

Pre Catalina将别名添加到~/.bash_profile

  • 这应该是最重要的答案。 (2认同)

小智 9

或者创建一个名为 .bash_profile 的符号链接指向您的 .bashrc

ln -s .bashrc .bash_profile
Run Code Online (Sandbox Code Playgroud)

  • 在较新版本的 OS X 上,终端可能使用 ZSH,这意味着应使用“.zshrc”而不是“.bash_profile”。 (5认同)

小智 6

我尝试使用该解决方案来更新 . bash_profile.bashrc但这不起作用,因为 Catalina 正在使用 zsh。 所以,我需要创建一个新文件~/.zprofile,在那里添加我的别名,然后使用命令“ source ~/.zprofile ”使其永久化。