Alias 无法在 Mac 上永远保存?

Alias can't save forever on Mac?

我想创建我自己的别名来使一些命令更多 simpler.I 添加 alias ll='ls -l'~/.bashrc 中,像这样:

 ANDROID_NAME=/Users/smy/Library/Android/sdk
 PYTHONPATH=/Library/Python/2.7/site-packages:$PYTHONPATH
 PATH=$ANDROID_NAME/platform-tools:$PYTHONPATH:$PATH

 export ANDROID_HOME
 export PYTHONPATH
 export PATH

 #alias 
 alias ll='ls -l'

当我第一次将这个别名添加到这个文件时,我执行 source 命令,就像这样:

source ~/.bashrc

然后在这个命令window中,它可以工作,但是当我创建一个新命令window时,它无法识别ll别名,即当我执行ll,存在这样的错误:

-bash: ll: command not found

当我输入 source ~/.bashrc 时,它会起作用。

所以我的问题是: 为什么每次输入别名都无法识别,为什么我必须执行 source 命令才能在打开新命令 window 时使它起作用,以及如何解决这个问题。 我正在研究 mac,任何人都可以教我这方面的知识,谢谢!

您需要使用 ~/.bash_profile~/.profile 登录 shell,而不是 ~/.bashrc。来自 documentation:

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.

和:

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

打开新终端 window/tab 时,shell 应作为 login shell.

打开

将你需要的别名插入~/.bash_profile和~/.bash 转到终端 -> 首选项 -> 将军 -> Shell 打开 -> 命令(完整路径) -> /bin/bash

只有一件事对我有用,我还没有看到它被提及。

除了

  1. 确保我的 .bash_profile 文件包含相关 alias/aliases
  2. 确保在终端>首选项>常规window中选择的Shell opens with选项是/bin/bash

我需要确保每次打开新的 window 时使用 bash -l 加载 bash 配置文件。可能花了 3000 万寻找这个,所以希望它能帮助那些更新到最新版本 MacOS 的人(就像你今天真正的一样)!