If you often use the Linux command line, you may encounter some problems, such as entering some long or complex commands, or often entering the wrong commands. Is there a way to perform what you want faster and more conveniently? The answer is yes, it is shell alias. Shell alias is a technique that allows you to customize commands by replacing a complete command with a short name, or by adding commonly used options to an existing command. In this article, we'll cover how to create and use shell aliases, as well as their advantages and limitations.
alias gg='git status'
Simple, yes. Efficient? You can't imagine. If you count the number of times git status is called every day, you might be a little shocked. This is a very common command in Git repositories. It shows which files are ready for commit, which branch you are on, and a lot of other valuable information.
Typing git status might not seem too crazy, but see how long it takes? Look at the way your fingers dance across the keyboard. There's a space, and the characters themselves are spaced out in a specific pattern that's prone to error. Once you mess up, you need to start over. So much time wasted. Just abbreviate it.
Typing gg is much faster, if you make a typo then maybe consider a typing course...
alias pll='pylint -f colorized'
Whether you love it or hate it, code inspectors are essential. Like it or not, no engineer is perfect. We all make mistakes at some point. You need to perform inspections on your code, so making it simple and easy is crucial.
I write a lot of Python code, so I rely heavily on Pylint for code inspection. I also like to run Pylint with colored output, sometimes with other custom parameters. It's not fun typing this stuff over and over again. Set it to a short abbreviation and it's now even more interesting.
You can pass different format types, ignore files, checkers, and different .rc files. Basically, anything you can pass to a normal command can be put in an abbreviation. That's the power of it, because you can set up the code inspector to your liking without having to remember or re-enter all those options.
alias ecr_login='aws ecr get-login-password --region | docker login --username AWS --password-stdin '
If you often use Docker images and also use ECR, then this unique command will be better than bread. Logging into ECR is very tedious. You have to chain all these commands together and use specific credentials to let Docker log in. It's a real pain if you don't remember that command. Then you have to look it up, which wastes a lot of time.
Just set the login command to a concise and clear alias. If you work in multiple ECR regions, you can even alias based on region. Set up a login alias for the East Coast, the West Coast and even beyond. The world is your oyster, now you can log in quickly and easily.
Don't forget to add your own zone and ECR URI to the above command to make it work properly.
alias apb='cd && ansible-playbook -i
For me, running Ansible playbooks is a daily routine. I run multiple plays every day, often repeatedly. Especially when I'm building new roles or struggling with conditional logic in Ansible. Who doesn't love YAML as code?
能够使用我需要的基本标志运行Ansible非常重要。重新输入一长串参数需要很长时间。当然,你可以按上箭头重用之前执行的命令,但这并不总是最方便的选项。如果你转而切换到编辑文件,现在又要按两次上箭头。这很容易在开发过程中被忽略,然后你又得重新输入一遍。
只需给它取个别名。
这还可以让你有机会确保你是否在使用ansible.cfg文件(如果有的话)。在上面的命令别名中,我们进入了我们的仓库并在该上下文中运行。这是因为我们的Ansible配置位于那里。一旦你进入那个目录,它会自动使用该配置。
如果有其他的清单文件,你也可以传入它们。如果你有一些跨多个环境的全局清单,这将非常有帮助。
5、缩短常用命令
alias tf='terraform' alias k='kubectl'
这是我最近开始实施的最后一个建议。起初我觉得这并不是必要的,但一旦你开始使用一两个字母的命令而不是七八个字母的命令,就会有很大的不同。我每天都经常使用terraform和kubectl。
这些命令是另一个例子,它们的名称很烦人。它们容易出错,而将它们设为别名非常简单。给自己一个方便,现在就将它们缩短,这样你就可以忘记它们的拼写。
我还鼓励你查看其他你经常输入的单词命令。可能有一些你一遍又一遍地输入的命令可以从缩短中受益。
现在,去给命令设置别名吧!
在本文中,我们学习了如何使用 shell alias 来简化 Linux 命令行操作。我们了解了如何定义和删除 alias,如何查看已有的 alias,以及如何在不同的环境中使用 alias。我们还介绍了一些常用的 alias 示例,以及 shell alias 的一些注意事项和局限性。通过使用 shell alias,我们可以提高 Linux 命令行效率,节省时间和精力,更好地完成我们的工作。
The above is the detailed content of How to use shell alias to improve Linux command line efficiency. For more information, please follow other related articles on the PHP Chinese website!