bash .bash_profile 中的别名无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28289131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:51:27  来源:igfitidea点击:

Aliases in .bash_profile not working properly

macosbashterminal.bash-profile

提问by Adrian M

I have been trying to alter the .bash_profile that is in my root directory, but have been running into some problems. I am on OS X, Yosemite, on a Macbook Pro. As I understand it, the .bash_profile file contains the script that is called automatically whenever the Terminal app is opened and the bash shell starts. This is what I currently have written in that file:

我一直在尝试更改根目录中的 .bash_profile,但遇到了一些问题。我在 OS X、优胜美地和 Macbook Pro 上。据我了解,.bash_profile 文件包含在终端应用程序打开和 bash shell 启动时自动调用的脚本。这是我目前在该文件中所写的内容:

PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

This works perfectly fine. However, I want to add an alias (right underneath the above two lines) as follows:

这工作得很好。但是,我想添加一个别名(在上面两行的正下方),如下所示:

alias test='cd ..'

However, when I save this and start up the Terminal, I get the following message:

但是,当我保存它并启动终端时,我收到以下消息:

-bash: alias: ..": not found

Replacing the single quotes with double quotes doesn't help, nor does taking them away altogether. Curiously however, the following alias works:

用双引号替换单引号无济于事,也不会将它们完全去掉。然而,奇怪的是,以下别名有效:

alias c=clear

When I type c into the terminal, it clear the screen, as you would expect. However, if I instead entered this line with quotes in the bash profile as:

当我在终端中输入 c 时,它会清除屏幕,正如您所期望的那样。但是,如果我改为在 bash 配置文件中使用引号输入此行:

alias c='clear'

Then I will get the following whenever I enter c into the Terminal:

然后,每当我在终端中输入 c 时,我都会得到以下信息:

-bash: 'clear': command not found

Note that I do not get an error message on startup for this alias.

请注意,我在启动时没有收到此别名的错误消息。

What am I doing wrong? Is there a setting I need to change somewhere to get aliases to work properly? I have seen previous examples of aliases and they simply do not work for me.

我究竟做错了什么?是否需要在某处更改设置以使别名正常工作?我看过以前的别名示例,但它们对我不起作用。

回答by nodejh

It looks like shell is not accessing your .bash_profile when logging via terminal.

通过终端登录时,shell 似乎没有访问您的 .bash_profile。

.bash_profileis a config file of bash shell. When bash shell starting, shell will execute the commands in .bash_profile. But there are many kinds of shells, and different shells execute different config file.

.bash_profile是 bash shell 的配置文件。当 bash shell 启动时,shell 将执行 .bashrc 中的命令.bash_profile。但是shell有很多种,不同的shell执行不同的配置文件。

Terminal is a software to receive user input, shell will execute commands.You can use cat /etc/shellsto list acceptable shells. For example:

Terminal 是一个接收用户输入的软件,shell 会执行命令。你可以cat /etc/shells用来列出可接受的shell。例如:

$ cat /etc/shells
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

The default shell is bash shell on Mac OX. But if you have installed zsh, the default shell is zsh, when zsh shell starting, shell will find out the file named .zshrc, and execute the commands in .zshrc.

Mac OX 上的默认 shell 是 bash shell。但是,如果你已经安装的zsh,默认的shell是zsh的,当zsh的外壳开始,外壳会发现命名的文件.zshrc,并在执行命令.zshrc

You can use echo $SHELLdo you use:

你可以使用echo $SHELL你使用:

$ echo $SHELL
/bin/bash
-> echo $SHELL
/bin/zsh

If your default shell is zsh, .bash_profiledon't work. The config file of zsh is .zshrc. And I guess your problem is that your default shell is not bash shell. For example, if your default shell is zsh, you should config the .zshrc, just add

如果您的默认 shell 是 zsh,.bash_profile请不要工作。zsh 的配置文件是.zshrc. 我猜你的问题是你的默认 shell 不是 bash shell。例如,如果你的默认 shell 是 zsh,你应该配置.zshrc,只需添加

PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

or other config to ~/.zshrc.

或其他配置到~/.zshrc.

Then source ~/.zshrc, and the config will work immediately.

然后source ~/.zshrc,配置将立即生效。

回答by Kannan Ramamoorthy

For me, it didn't work in the same terminal. I had to open a new one to get it work.

对我来说,它在同一个终端中不起作用。我不得不打开一个新的才能让它工作。

回答by Ujjal Suttra Dhar

Wrapping the command with double quotation worked for me. I was trying with every possible way mentioned in this thread and none of them worked. Then I replaced single quote to double and that worked.

用双引号包裹命令对我有用。我正在尝试使用此线程中提到的所有可能方法,但都没有奏效。然后我将单引号替换为双引号并且有效。

    alias mysql_start="sudo /path/to/server/mysql.server start"

回答by user2012643

The killer for me was space and single quotes. alias test="cd .." worked. Don't put any spaces between alias_name=

对我来说杀手是空格和单引号。alias test="cd .." 工作。不要在 alias_name= 之间放置任何空格

回答by Dale_Reagan

User level 'system' files need to contain 'plain text'. How to set/configure this for your 'editor-of-choice' can vary (I don't use a Mac so I'm not much help with that.) Soo,

用户级别的“系统”文件需要包含“纯文本”。如何为您的“首选编辑器”设置/配置它可能会有所不同(我不使用 Mac,所以我对此没有太大帮助。) Soo,

  1. create your profile 'from the shell' by appending lines directly, i.e. remove the 'bad lines and then:

    echo "some command string" >> ~/path/bash_profile

  2. use 'vi' or 'vim' ## should be 'safe'

  3. review your file via:

    cat -v ~/path/bash_profle | more ## see any 'funny chars'? or
    cat -ve ~/path/bash_profle | more ## see any 'funny chars'? or

  1. 通过直接附加行来“从外壳”创建您的配置文件,即删除“坏行”,然后:

    回声“一些命令字符串”>> ~/path/bash_profile

  2. 使用 'vi' 或 'vim' ## 应该是 'safe'

  3. 通过以下方式查看您的文件:

    cat -v ~/path/bash_profle | 更多## 看到任何“有趣的字符”?或
    cat -ve ~/path/bash_profle | 更多## 看到任何“有趣的字符”?或者

or

或者

vi ~/path/bash_profle # then set 'control codes' to 'on'
set list ## see any 'funny chars' for your 'quotes'?

回答by Akshay Mahajan

What I realised is that Mac has option for smart quotes and dashes. alias ..='cd ../'is different from alias ..=‘cd ../‘, where the former works but the latter doesn't.

我意识到 Mac 可以选择智能引号和破折号。 alias ..='cd ../'与 不同alias ..=‘cd ../‘,前者有效,后者无效。

回答by Tran Quoc Vuong

You can run alias custom auto startup in ~/.bash_profile or ~/.alias_file... by paste file name alias in ~/.bashrc if you use bash or ~/.zshrc if you use zsh.

您可以在 ~/.bash_profile 或 ~/.alias_file... 中运行别名自定义自动启动...通过在 ~/.bashrc 中粘贴文件名别名(如果使用 bash 或 ~/.zshrc 如果使用 zsh。

Ex:

前任:

if [ -s ~/.bash_profile ]; then
    source ~/.bash_profile;
fi

回答by Ankit Agarwal

Make sure that the alias commands are together in the .bash_profiletoo. That is what was wrong with mine.

确保别名命令也放在一起.bash_profile。那是我的错。

Also, if you just type in alias in the terminal, it should list all the known alias commands, so if you don't see your command you know something is wrong.

此外,如果您只是在终端中输入别名,它应该列出所有已知的别名命令,因此如果您没有看到您的命令,您就知道有问题。

回答by Ankit Agarwal

I fixed this by editing my .bash_profile in vim or nano something about text edited messed it up unsure why.

我通过在 vim 或 nano 中编辑我的 .bash_profile 来解决这个问题,一些关于文本编辑的东西搞砸了,不知道为什么。

I tried to edit in in notepad just using "open .bash_profile" however something about the symbols translated wrong. So you have to either "vim .bash_profile" or "nano .bash_profile"

我试图在记事本中仅使用“open .bash_profile”进行编辑,但是有关符号翻译的某些内容是错误的。所以你必须“vim .bash_profile”或“nano .bash_profile”

Doing this corrected this problem for me.

这样做为我纠正了这个问题。

回答by fohrums

If alias is not recognizing, first identify what shell you're using when you open up your terminal or commandlineprompt echo $SHELL. For me it's /bin/bashso i'll input my aliases in ~/.bash_profile

如果别名无法识别,请在打开终端或 commandlineprompt 时首先确定您使用的 shell echo $SHELL。对我来说它是/bin/bash所以我会输入我的别名~/.bash_profile

Take note, the file ~/.bash_profile can store both aliases to call out during a terminal session and autorun those same aliases upon opening up a terminal session. The example below would be to open up a file to edit by text via vim-software. You may change it other text-editorsas you wish. You can see the difference by closure-method.

请注意,文件 ~/.bash_profile 可以存储在终端会话期间调用的两个别名,并在打开终端会话时自动运行这些相同的别名。下面的例子是打开一个文件,通过vim-software进行文本编辑。您可以根据需要更改其他文本编辑器。您可以通过闭包方法看到差异。

ALIAS CALLOUT (uses parenthesis)

别名标注(使用括号

  • alias editbp="vim ~/.bash_profile"
  • 别名 editbp="vim ~/.bash_profile"

ALIAS AUTORUN (uses tilda)

ALIAS AUTORUN(使用 tilda

  • alias editbp=`vim ~/.bash_profile`
  • 别名 editbp=`vim ~/.bash_profile`