How do I create a Bash alias?

2019-01-09 23:56发布

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.

标签: macos bash shell
10条回答
劫难
2楼-- · 2019-01-10 00:12

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

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

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

查看更多
戒情不戒烟
3楼-- · 2019-01-10 00:17

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

查看更多
再贱就再见
4楼-- · 2019-01-10 00:18

I just open zshrc with sublime, and edit it.

subl .zshrc

And add this on sublime:

alias blah="/usr/bin/blah"

Run this in terminal:

source ~/.bashrc

Done.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-10 00:19

In my .bashrc file the following lines were there by default:

# 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.

查看更多
趁早两清
6楼-- · 2019-01-10 00:19
cd /etc
sudo vi bashrc

Add the following like:

alias ll="ls -lrt"

Finally restart Terminal.

查看更多
家丑人穷心不美
7楼-- · 2019-01-10 00:20

You probably want to edit the .bashrc file in your home directory.

查看更多
登录 后发表回答