我在OSX上,我需要把这样的东西,别名blah=“/usr/bin/blah”在配置文件中,但我不知道配置文件在哪里。


当前回答

在root - ex用户下创建bash_profile

/user/username/.bash_profile

打开的文件

vim ~ / . bash_profile

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

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

在新终端中输入mydir -它应该会打开

/user/username/Documents/dirname/anotherdir

其他回答

在root - ex用户下创建bash_profile

/user/username/.bash_profile

打开的文件

vim ~ / . bash_profile

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

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

在新终端中输入mydir -它应该会打开

/user/username/Documents/dirname/anotherdir

我需要运行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

我认为这是正确的方式:

1)去终点站。开放的~ / . bashrc。如果不存在则添加

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

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

3)增加新的别名 -编辑.bash_aliases文件并重启终端或打印source ~/.bash_aliases - print echo "alias clr='clear'" >> ~/。Bash_aliases && source ~/。Bash_aliases你的别名是别名clr='clear'。

4)添加行源~/。bash_alias到~/。bash_profile文件。它需要在终端的每个init中加载别名。

您可以在启动脚本文件中添加别名或函数。通常是主目录下的.bashrc、.bash_login或.profile文件。

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


如果我没记错的话,当我买了我的Mac时,.bash_login文件并不在那里。我必须为自己创建它,这样我就可以把提示信息、别名、函数等放在里面。

如果你想创建一个,以下是步骤:

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

MacOS卡特琳娜及以上

苹果将默认shell转换为zsh,因此配置文件包含~/。Zshenv和~/.zshrc。这就像~/。Bashrc,但zsh。只需编辑文件并添加您需要的内容;每次打开一个新的终端窗口时,都应该引用它:

纳米~ - zshenv。 别名py = python)

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

该文件似乎无论在什么情况下(登录、非登录或脚本)都要执行,因此似乎比~/更好。zshrc中。

高海拔及更早

默认shell是bash,您可以编辑文件~/。Bash_profile和添加别名:

纳米~ / . bash_profile 别名py = python

然后按ctrl+x, y, enter保存。有关这些配置的更多信息,请参阅这篇文章。在~/中使用别名设置会更好一些。Bashrc,然后source ~/。~/.bash_profile中的Bashrc。在~ /。Bash_profile看起来就像这样:

来源 ~/.巴什尔克