如何创建 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
How do I create a Bash alias?
提问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 alias
or a function
in your startup script file. Usually this is .bashrc
, .bash_login
or .profile
file in your home directory.
您可以在启动脚本文件中添加alias
或function
。通常这是主目录中的.bashrc
,.bash_login
或.profile
文件。
Since these files are hidden you will have to do an ls -a
to 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_login
file 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 info
,alias
,functions
等它。
Here are the steps if you would like to create one:
如果您想创建一个,请执行以下步骤:
- Start up Terminal
- Type
cd ~/
to go to your home folder - Type
touch .bash_profile
to create your new file. - Edit
.bash_profile
with your favorite editor (or you can just typeopen -e .bash_profile
to open it in TextEdit. - Type
. .bash_profile
to reload.bash_profile
and update any alias you add.
- 启动终端
- 键入
cd ~/
以转到您的主文件夹 - 键入
touch .bash_profile
以创建新文件。 - 编辑
.bash_profile
用你喜欢的编辑器(或者您也可以只open -e .bash_profile
在TextEdit中打开它。 - 键入
. .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 .bashrc
file 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_aliases
is 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 .bashrc
file, 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 ~/.bashrc
and 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 ~/.bashrc
file 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
- Go to home
- Open .bashrc
Create alias at bottom of the file
alias alias_name='command to do' eg: alias cdDesktop='cd /Desktop'
Save the file
source .bashrc
source ~/.bashrc
Open terminal (Ctrl+Alt+T) & type cdDesktop & press enter
- 回家
- 打开 .bashrc
在文件底部创建别名
alias alias_name='command to do' eg: alias cdDesktop='cd /Desktop'
保存文件
源.bashrc
source ~/.bashrc
打开终端 (Ctrl+Alt+T) 并输入 cdDesktop 并按 Enter
回答by SiegeX
If you put blah="/usr/bin/blah"
in your ~/.bashrc
then you can use $blah
in your login shell as a substitute for typing /usr/bin/blah
如果你blah="/usr/bin/blah"
输入你的~/.bashrc
then 你可以$blah
在你的登录 shell 中使用作为输入的替代/usr/bin/blah
回答by ghoti
You probably want to edit the .bashrc
file in your home directory.
您可能想要编辑.bashrc
主目录中的文件。