如何创建Bash别名?

时间:2012-01-23 06:15:33

标签: macos bash shell

我在OSX上,我需要在配置文件中添加这样的alias blah="/usr/bin/blah",但我不知道配置文件在哪里。

16 个答案:

答案 0 :(得分:245)

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

由于隐藏了这些文件,您必须执行ls -a列出它们。如果你没有,你可以创建一个。


如果我没记错的话,当我买了我的Mac时,.bash_login文件就不存在了。我必须为自己创建它,以便我可以将prompt infoaliasfunctions等放入其中。

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

  1. 启动终端
  2. 输入cd ~/转到您的主文件夹
  3. 输入touch .bash_profile以创建新文件。
  4. 使用您喜欢的编辑器修改.bash_profile(或者您只需键入open -e .bash_profile即可在TextEdit中打开它。
  5. 键入. .bash_profile以重新加载.bash_profile并更新您添加的任何别名。

答案 1 :(得分:28)

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

查看有关不同配置文件的详细信息以及何时使用它们: What's the difference between .bashrc, .bash_profile, and .environment?

与OSX相关:About .bash_profile, .bashrc, and where should alias be written in?

答案 2 :(得分:23)

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

java.lang.Character.isAlphabetic(int)

并在sublime上添加:

subl .zshrc

在终端中运行:

alias blah="/usr/bin/blah"

完成。

答案 3 :(得分:14)

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

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

答案 4 :(得分:7)

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

我认为最好的办法就是让所有内容都在~/.bashrc

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

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

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

答案 5 :(得分:7)

cd /etc
sudo vi bashrc

添加以下内容:

alias ll="ls -lrt"

最后重启终端。

答案 6 :(得分:6)

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

    @date = DateTime.now   
    @date.beginning_of_month
    @date.end_of_month
    
  4. 保存文件

  5. source .bashrc

    alias alias_name='command to do'
    eg: alias cdDesktop='cd /Desktop'
    
  6. 打开终端(Ctrl + Alt + T)&键入cdDesktop&按enter

答案 7 :(得分:3)

如果您将blah="/usr/bin/blah"放入~/.bashrc,那么您可以在登录shell中使用$blah代替/usr/bin/blah

答案 8 :(得分:3)

它在macOS Majave上对我有效

您可以执行一些简单的步骤:

1)打开终端

2)sudo nano /.bash_profile

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

完成。使用并享受!

答案 9 :(得分:2)

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

答案 10 :(得分:1)

MacOS Catalina及以上

Apple just switched的默认外壳为 zsh ,因此配置文件包括~/.zshenv~/.zshrc。就像~/.bashrc一样,但是对于zsh。只需编辑文件并添加所需内容即可;每当您打开新的终端窗口时,都应提供该来源:

nano ~/.zshenv alias py=python

然后按ctrl + x,y,然后输入以保存。

该文件似乎无论执行什么操作(登录,非登录或脚本)都可以执行,因此比~/.zshrc文件要好。

High Sierra及更早版本

默认shell是bash,您可以编辑文件~/.bash_profile并添加别名:

nano ~/.bash_profile alias py=python

然后按ctrl + x,y,然后输入保存。有关这些配置的更多信息,请参见this post。最好在~/.bashrc中使用别名进行设置,然后从~/.bashrc中获取~/.bash_profile。在~/.bash_profile中,它看起来像:

source ~/.bashrc

答案 11 :(得分:1)

对于macOS Catalina用户:

步骤1:创建或更新.zshrc文件

vi ~/.zshrc

第2步:添加别名行

alias blah="/usr/bin/blah"

第3步:源.zshrc

source ~/.zshrc 

第4步:通过在命令提示符下键入alias来检查您是否为别名

alias

答案 12 :(得分:0)

要创建永久别名快捷方式,请将其放在.bash_profile文件中,然后将.bashrc文件指向.bash_profile文件。请执行以下步骤(我正在创建一个名为bnode的别名命令,以在ES6代码上运行babel transpiler):

  1. 转到终端命令提示符,然后键入“ cd”(这将 您到主目录。注意:即使您的程序文件可能 位于“ D:驱动器”上,“。bash”文件可能位于 您的“ C:驱动器”)
  2. 要查看主目录的位置,请键入“ pwd”(这将显示主目录路径以及.bash文件可能位于的位置)
  3. 查看所有点“。”主目录中的文件,键入“ ls -la”(这将显示所有文件,包括隐藏的点“。”文件)
  4. 您将看到2个文件:“。bash_profile”和“ .bashrc”
  5. 在VS Code编辑器或IDE中打开.bashrc文件,并在第一行中输入“ source〜/ .bash_profile”(将.bashrc文件指向.bash_profile)
  6. 在VS Code编辑器中打开.bash_profile文件,然后输入“ alias bnode ='。/ node_modules / .bin / babel-node'”(以创建永久性的bnode快捷方式以作为bash命令执行)
  7. 保存并关闭两个文件
  8. 现在打开要执行的文件(index.js),然后在终端命令提示符下打开并使用命令“ bnode index.js”运行文件。
  9. 现在您的index.js文件将执行,但是在.bash_profile文件中创建bnode别名之前,您会收到错误“ bash:未找到bnode命令”,并且它将无法识别某些ES6代码并给出错误。
  10. 了解点文件的有用链接:https://dotfiles.github.io/

希望这会有所帮助!祝你好运!

答案 13 :(得分:0)

我认为这是正确的方法:

1)转到终端。 open ~/.bashrc。如果不存在则添加

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

2)open ~/.bash_aliases。如果不存在:touch ~/.bash_aliases && open ~/.bash_aliases

3)而是添加新别名
-编辑.bash_aliases文件并重新启动终端或打印source ~/.bash_aliases
-打印echo "alias clr='clear'" >> ~/.bash_aliases && source ~/.bash_aliases,您的别名为alias clr='clear'

4)将source ~/.bash_aliases行添加到~/.bash_profile文件中。它需要在终端的每个init中load aliases

答案 14 :(得分:0)

在您的用户root-ex

上创建一个bash_profile
/user/username/.bash_profile

打开文件

vim〜/ .bash_profile

添加别名作为ex。 (保存并退出)

alias mydir="cd ~/Documents/dirname/anotherdir"

在新终端中,只需键入mydir-它应打开

/user/username/Documents/dirname/anotherdir

答案 15 :(得分:0)

我需要运行Postgres数据库并为此创建一个别名。提供的工作如下:

$ nano ~/.bash_profile 

# in the bash_profile, insert the following texts:

alias pgst="pg_ctl -D /usr/local/var/postgres start"
alias pgsp="pg_ctl -D /usr/local/var/postgres stop"


$ source ~/.bash_profile 

### This will start the Postgres server 
$ pgst

### This will stop the Postgres server 
$ pgsp
相关问题