如何创建 Bash 别名?

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

How do I create a Bash alias?

macosbashshell

提问by fancy

I'm on OSX and I need to put something like this, alias blah="/usr/bin/blah"in a config file but I don't know where the config file is.

我在 OSX 上,我需要把这样的东西alias blah="/usr/bin/blah"放在一个配置文件中,但我不知道配置文件在哪里。

回答by jaypal singh

You can add an aliasor a functionin your startup script file. Usually this is .bashrc, .bash_loginor .profilefile in your home directory.

您可以在启动脚本文件中添加aliasfunction。通常这是主目录中的.bashrc,.bash_login.profile文件。

Since these files are hidden you will have to do an ls -ato list them. If you don't have one you can create one.

由于这些文件是隐藏的,您将不得不ls -a列出它们。如果你没有,你可以创建一个。



If I remember correctly, when I had bought my Mac, the .bash_loginfile wasn't there. I had to create it for myself so that I could put prompt info, alias, functions, etc. in it.

如果我没记错的话,当我购买 Mac 时,该.bash_login文件不存在。我不得不为自己创建它,这样我可以把prompt infoaliasfunctions等它。

Here are the steps if you would like to create one:

如果您想创建一个,请执行以下步骤:

  1. Start up Terminal
  2. Type cd ~/to go to your home folder
  3. Type touch .bash_profileto create your new file.
  4. Edit .bash_profilewith your favorite editor (or you can just type open -e .bash_profileto open it in TextEdit.
  5. Type . .bash_profileto reload .bash_profileand update any alias you add.
  1. 启动终端
  2. 键入cd ~/以转到您的主文件夹
  3. 键入touch .bash_profile以创建新文件。
  4. 编辑.bash_profile用你喜欢的编辑器(或者您也可以只open -e .bash_profile在TextEdit中打开它。
  5. 键入. .bash_profile以重新加载.bash_profile和更新您添加的任何别名。

回答by Mike Gardiner

On OS X you want to use ~/.bash_profile. This is because by default Terminal.app opens a login shell for each new window.

在 OS X 上你想使用 ~/.bash_profile。这是因为默认情况下 Terminal.app 会为每个新窗口打开一个登录 shell。

See more about the different configuration files and when they are used here: What's the difference between .bashrc, .bash_profile, and .environment?

在此处查看有关不同配置文件以及它们何时使用的更多信息: .bashrc、.bash_profile 和 .environment 之间有什么区别?

and in relation to OSX here: About .bash_profile, .bashrc, and where should alias be written in?

和这里的 OSX 相关:关于 .bash_profile、.bashrc,以及别名应该写在哪里?

回答by Gui Yoshi

I just open zshrc with sublime, and edit it.

我只是用 sublime 打开 zshrc,然后编辑它。

subl .zshrc

And add this on sublime:

并在 sublime 上添加:

alias blah="/usr/bin/blah"

Run this in terminal:

在终端中运行:

source ~/.bashrc

Done.

完毕。

回答by jcollado

In my .bashrcfile the following lines were there by default:

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

Hence, in my platform .bash_aliasesis the file used for aliases by default (and the one I use). I'm not an OS X user, but I guess that if you open your .bashrcfile, you'll be able to identify what's the file commonly used for aliases in your platform.

因此,在我的平台.bash_aliases中,默认情况下是用于别名的文件(也是我使用的文件)。我不是 OS X 用户,但我想如果你打开你的.bashrc文件,你将能够识别你的平台中常用的别名文件是什么。

回答by user3613987

cd /etc
sudo vi bashrc

Add the following like:

添加以下内容,例如:

alias ll="ls -lrt"

Finally restart Terminal.

最后重启终端。

回答by ultimatum

It works for me on macOS Majave

它在 macOS Majave 上对我有用

You can do a few simple steps:

您可以执行几个简单的步骤:

1) open terminal

1) 打开终端

2) sudo nano /.bash_profile

2) sudo nano /.bash_profile

3) add your aliases, as example:

3)添加您的别名,例如:

# some aliases
alias ll='ls -alF'
alias la='ls -A'
alias eb="sudo nano ~/.bash_profile && source ~/.bash_profile"
#docker aliases
alias d='docker'
alias dc='docker-compose'
alias dnax="docker rm $(docker ps -aq)"
#git aliases
alias g='git'
alias new="git checkout -b"
alias last="git log -2"
alias gg='git status'
alias lg="git log --pretty=format:'%h was %an, %ar, message: %s' --graph"
alias nah="git reset --hard && git clean -df"
alias squash="git rebase -i HEAD~2"

4) source /.bash_profile

4) source /.bash_profile

Done. Use and enjoy!

完毕。使用和享受!

回答by kmikael

The config file for scripts and programs is ~/.bashrcand the config file that gets loaded when you use Terminal is ~/.bash_login.

脚本和程序~/.bashrc的配置文件是,使用终端时加载的配置文件是~/.bash_login.

I think the best way is to just have everything in ~/.bashrc.

我认为最好的方法是将所有内容都放在~/.bashrc.

For your specific question just enter (this will overwrite any existing ~/.bashrc):

对于您的特定问题,只需输入(这将覆盖任何现有的 ~/.bashrc):

echo "alias blah=\"/usr/bin/blah\"" >>~/.bashrc

into the Terminal and a ~/.bashrcfile will be created with your new alises. After that just edit the file to add new aliases, functions, settings etc.

进入终端,~/.bashrc将使用您的新别名创建一个文件。之后只需编辑文件以添加新的别名、功能、设置等。

回答by Pranav V R

  1. Go to home
  2. Open .bashrc
  3. Create alias at bottom of the file

    alias alias_name='command to do'
    eg: alias cdDesktop='cd /Desktop'
    
  4. Save the file

  5. source .bashrc

    source ~/.bashrc
    
  6. Open terminal (Ctrl+Alt+T) & type cdDesktop & press enter

  1. 回家
  2. 打开 .bashrc
  3. 在文件底部创建别名

    alias alias_name='command to do'
    eg: alias cdDesktop='cd /Desktop'
    
  4. 保存文件

  5. 源.bashrc

    source ~/.bashrc
    
  6. 打开终端 (Ctrl+Alt+T) 并输入 cdDesktop 并按 Enter

回答by SiegeX

If you put blah="/usr/bin/blah"in your ~/.bashrcthen you can use $blahin your login shell as a substitute for typing /usr/bin/blah

如果你blah="/usr/bin/blah"输入你的~/.bashrcthen 你可以$blah在你的登录 shell 中使用作为输入的替代/usr/bin/blah

回答by ghoti

You probably want to edit the .bashrcfile in your home directory.

您可能想要编辑.bashrc主目录中的文件。