我的 Bash 别名不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11548093/
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-09 22:27:15  来源:igfitidea点击:

My Bash aliases don't work

bashshellscriptingalias

提问by steve_gallagher

I'm not sure why but my Bash aliases don't seem to work. Here is my .bashrcfile

我不知道为什么,但我的 Bash 别名似乎不起作用。这是我的.bashrc文件

    # v 0.0.1 - 7/03/12

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting

# expanding history to 10000 commands
export HISTSIZE=10000

# don't store repeated commands more than once
export HISCONTROL=ignoredups

# where to look for Java
export JAVA_HOME=/Library/Java/Home

# tomcat server configuration
export CATALINA_HOME=/usr/local/apache-tomcat-6.0.35

# default editor
export EDITOR=vim

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Here is my .bash_aliasesfile

这是我的.bash_aliases文件

# v 0.0.1 - 7/03/12

# aliases for directory traversal
alias ..='cd ../'
alias ...='cd ../../'
alias ....='cd ../../../'

alias gs='git status '
alias ga='git add '
alias gb='git branch '
alias gc='git commit'
alias gd='git diff'
alias go='git checkout '
alias gk='gitk --all&'
alias gx='gitx --all'

alias got='git '
alias get='git '

回答by Sean Bright

Add this to the end of your .bashrc:

将此添加到您的末尾.bashrc

if [ -f $HOME/.bash_aliases ]
then
  . $HOME/.bash_aliases
fi

回答by user2084149

I had a similar problem recently. The solution appeared to be closing ALL open shells (root and user; I didn't notice that I was running a minimized root shell at the time while editing my user .bashrc and .bash_aliases files). The .bash_aliases file then seemed to get read.

我最近遇到了类似的问题。解决方案似乎是关闭所有打开的 shell(root 和用户;我没有注意到我当时在编辑我的用户 .bashrc 和 .bash_aliases 文件时正在运行最小化的 root shell)。然后 .bash_aliases 文件似乎被读取了。

回答by Matovu Ronald

By default

默认情况下

if [ -f ~/.bash_aliases ]; then
.  ~/.bash_aliases
fi

These are available in your .bashrc file in ubuntu 18,19 Actually the problem is sourcing the files, therefore source both files by runing the commands below. I faced the same issues and that is how i solved it.

这些在 ubuntu 18,19 中的 .bashrc 文件中可用。实际上问题在于获取文件,因此通过运行以下命令来获取这两个文件。我遇到了同样的问题,这就是我解决它的方法。

source ~/.bashrc
source ~/.bash_aliases

回答by chepner

Bash doesn't look for a file called .bash_aliases; you have to source it explicitly.

Bash 不会查找名为.bash_aliases;的文件。你必须明确地提供它。

Looking around a bit, it appears ~/.bash_aliasesis sourced from the default .bashrcon Ubuntu boxes; I don't have access to one to confirm. However, it is not a standard bashconfiguration file.

环顾四周,它似乎~/.bash_aliases来自.bashrcUbuntu 机器上的默认值;我无权访问一个来确认。但是,它不是标准bash配置文件。

回答by prodigerati

I recently installed RVM and changed my terminal profile to "run command as login shell". This disabled .bashrc from loading.

我最近安装了 RVM 并将我的终端配置文件更改为“作为登录 shell 运行命令”。这禁用了 .bashrc 加载。

Fix: edit -> profile preferences -> Title and Command -> Run command as a login shell (uncheck)

修复:编辑 -> 配置文件首选项 -> 标题和命令 -> 作为登录 shell 运行命令(取消选中)

Find this post for more information, fixed it for me.

找到这篇文章以获取更多信息,为我修复了它。

https://askubuntu.com/questions/161249/bashrc-not-executed-when-opening-new-terminal

https://askubuntu.com/questions/161249/bashrc-not-executed-when-opening-new-terminal

回答by Suraj Shrestha

Sometimes forgetting to source the bashrc also creates this problem. So after adding your aliases don't forget to source it.

有时忘记获取 bashrc 的源也会产生这个问题。所以在添加你的别名后不要忘记获取它。

source ~/.bashrc

回答by Squazic

You need to include the file. Example code to do so from a default .bashrcfile is below:

您需要包含该文件。从默认.bashrc文件执行此操作的示例代码如下:

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi