在Catalina(Mac OS)中为Shell添加别名

I recently started using a mac with the Catalina OS (the latest one). Late to the party, I have been impressed by the features so far.

我最近开始在Mac和Catalina OS(最新版本)上使用Mac。 晚会晚了,到目前为止,我对这些功能印象深刻。

However, I recently ran into a bit of trouble when attempting to add some of my command line aliases. Here is how I resolved the issue with the help of StackOverflow.

但是,最近在尝试添加一些命令行别名时遇到了麻烦。 这是我在StackOverflow的帮助下解决问题的方法。

To initially add my aliases, I edited the bash_profile config script, which is the method I had used for High Sierra.

为了最初添加别名,我编辑了bash_profile配置脚本,这是我用于High Sierra的方法。

I added a few of my favourite aliases and started a new shell session but received an error when trying to use one of the commands:

我添加了一些我喜欢的别名并启动了一个新的Shell会话,但是在尝试使用以下命令之一时收到错误消息:

zsh: command not found: gs

I guessed that the command was not working on account of a syntax error (possible a trailing space somewhere).

我猜想该命令由于语法错误而不起作用(可能在某处有尾随空格)。

In closer inspection, I noticed that zsh had replaced bash in the message.

在仔细检查中,我注意到zsh已替换了邮件中的bash

As it turns out, the version of shell used with the latest OS (Catalina) is now zsh, also known as Z Shell. To make this clearer, running bash as a command to switch profiles within the session will display the message below:

事实证明,与最新OS(Catalina)一起使用的shell版本现在是zsh,也称为Z Shell。 为了更清楚地说明这一点,运行bash作为在会话中切换配置文件的命令将显示以下消息:

The default interactive shell is now zsh.To update your account to use zsh, please run `chsh -s /bin/zsh`.For more details, please visit https://support.apple.com/kb/HT208050.

Arguably, the change is due to zsh being a more modern version of shell.

可以说,变化是由于zsh是Shell的更新版本。

After some searching on StackOverflow, I found that the solution was to add the aliases to the zsh equivalent of the config file. In my experience, I had to create the file itself:

在对StackOverflow进行一些搜索之后,我发现解决方案是将别名添加到config文件的zsh等效项中。 以我的经验,我必须创建文件本身:

touch ~/.zshrc

After that, I readded the commands from the bash_profile script to .zshrc. I started a new shell session and found that they were all working again successfully.

在那之后,我重新添加从命令bash_profile脚本.zshrc 。 我开始了一个新的Shell会话,发现它们都再次成功运行。

An alternative to this solution would be to change your systems shell profile back to bash. Doing so is possible using the change shell command:

此解决方案的替代方法是将您的系统Shell配置文件更改回bash。 使用change shell命令可以这样做:

chsh -s /bin/bash

Once you have changed back to bash, you should be able to start a new session, and any aliases saved in bash_profile should work as expected. Keep in mind that the version of bash installed by default is out of date:

更改回bash后,您应该能够开始新的会话,并且保存在bash_profile所有别名都应能按预期工作。 请记住,默认情况下安装的bash版本已过期:

bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)
Copyright (C) 2007 Free Software Foundation, Inc.

The latest version of bash is 5.0. It’s possible to read more about updating to the latest version here.

bash的最新版本是5.0。 可以在此处阅读有关更新到最新版本的更多信息。

Finally, you can also see all of the versions of shell available on your machine by running the following command:

最后,您还可以通过运行以下命令来查看计算机上可用的所有Shell版本:

cat /etc/shells

Despite having the option to switch versions being helpful, the advantages between versions seem relatively small. However, one of the most innovative features offered by Zsh, is the Oh My Zsh framework. Oh My Zsh is an opensource shell framework, which provides a ton of customisations to shell, in the form of themes and plugins. The plugins in particular, look quite helpful for making the command line quicker and more intuitive to use, which is arguably the point of aliases anyway.

尽管可以选择切换版本,但版本之间的优势似乎相对较小。 但是,Osh My Zsh框架是Zsh提供的最具创新性的功能之一。 噢,我的Zsh是一个开源Shell框架,它以主题和插件的形式为Shell提供了大量自定义设置。 尤其是插件,对于使命令行更快捷,更直观地使用而言非常有帮助,这无疑是别名的重点。

Thank you for reading and please let me know in the comments if this article was helpful in any way.

感谢您的阅读,如果本文对您有帮助,请在评论中告知我。

翻译自: https://medium.com/swlh/adding-aliases-to-shell-in-catalina-mac-os-dff9c514bb04

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值