bash 别名
Every developer likes a shortcut -- they're what make us more efficient in our work. Of course there are good shortcuts and bad shortcuts (lazy coding, lack of security review, etc.), but let's stick with the positive and talk about a good shortcut: bash aliases.
每个开发人员都喜欢捷径-这就是使我们工作效率更高的原因。 当然,有好的捷径和不好的捷径(懒惰的编码,缺乏安全审查等),但是让我们坚持肯定的做法,并谈论一个好的捷径:bash别名。
We all have commands that we execute regularly but aren't able to remember or simply don't care to constantly type, like removing all Docker images and containers or bringing down and instantly bringing up a docker project. Most commands we execute often each day are boilerplate; maybe one or two parameters change. Let's have a look at how easy it is to create aliases so you can be more productive!
我们都有定期执行的命令,但是它们不记得或者根本就不希望不断地键入命令,例如删除所有Docker映像和容器,或者关闭并立即启动Docker项目。 我们每天经常执行的大多数命令都是样板。 也许一两个参数发生变化。 让我们来看看创建别名有多么容易,这样您可以提高工作效率!
创建基本别名 (Creating a Basic Alias)
To create an alias, start by opening ~/.bash_profile
in any text editor you have available. The format for creating an alias is as follows:
要创建别名,请在可用的任何文本编辑器中打开~/.bash_profile
。 创建别名的格式如下:
# alias-name='command to do thing'
alias docker-refresh='docker-compose down && docker-compose up'
alias serve-dir='python -m SimpleHTTPServer'
I recommend naming your aliases in a way that wont conflict with existing or future executables. You could add a "namespace" or prefix to them, for example. To ensure the alias will work in your shell once you're done editing .bash_profile
, execute the following:
我建议以一种不会与现有或将来的可执行文件冲突的方式来命名您的别名。 例如,您可以为其添加“名称空间”或前缀。 为了确保别名在您编辑完.bash_profile
之后可以在您的外壳程序中运行,请执行以下操作:
source ~/.bash_profile
Creating a basic alias is fairly simple but what if your alias requires the use of arguments? That case is a bit different.
创建基本别名非常简单,但是如果您的别名需要使用参数呢? 这种情况有些不同。
带参数的别名 (Aliases with Arguments)
If the mostly boilerplate command we want to execute requires an argument or two, we'll need to use something more advanced that the basic bash alias format -- we'll need a function.
如果我们要执行的大多数样板命令需要一个或两个参数,则需要使用比基本bash别名格式更高级的东西-我们需要一个函数。
Let's say we want to execute a command that requires one argument -- we'd do something like this:
假设我们要执行一个需要一个参数的命令-我们将执行以下操作:
# Serve a directory on a given port
# https://davidwalsh.name/serve-directory-python
# $1 = port
# Example: servedir 8080
servedir() {
# Allow myself to change the port ($1)
python -m SimpleHTTPServer "$1"
}
# Scrape images with wget
# https://davidwalsh.name/scrape-images-wget
# $1 = url
# Example: scrapeimages https://davidwalsh.name/
scrapeimages() {
wget -nd -H -p -A jpg,jpeg,png,gif -e robots=off $1
}
# Remove audio from video
# https://davidwalsh.name/remove-audio-video
# $1 = source file
# $2 = destination
# Example: removeaudio myvideo.webm myvideo-silent.mp4
removeaudio() {
ffmpeg -i $1 -vcodec copy -an $2
}
Your functions can host contain any number of statements so that you can create advanced commands:
您的函数可以包含任意数量的语句,以便您可以创建高级命令:
startover() {
echo 'Killing everything'
rm -rf build/
docker-compose down
docker rm -f $(docker ps -a -q)
docker rmi -f $(docker images -q)
echo 'Everything killed, my lord'
}
Aliases are super easy to create and can increase your productivity. For years I was too lazy to take a few moments to create aliases and, looking back, it was a laziness that cost me. As you continue developing a new project, be sure to take a few moments to aliases for often-used commands -- you'll thank yourself later.
别名非常容易创建,可以提高您的生产率。 多年以来,我一直懒得花点时间来创建别名,而回想起来,那是一种懒惰使我付出了代价。 在继续开发新项目时,请确保花一些时间来使用常用命令的别名-稍后将感谢您。
bash 别名