请选择 进入手机版 | 继续访问电脑版
| |

PalletOneClub

github登录
 找回密码
 注册
查看: 274|回复: 6

Learn how to create custom bash commands in less than 4 minutes

[复制链接]

1

主题

2

帖子

52

积分

Member

Rank: 2

积分
52
发表于: 2018-08-03 10:32:05
| 显示全部楼层 |阅读模式
本帖最后由 palletclub 于 2018-08-03 10:36 编辑




Step 1 — Create a custom_aliases file


All aliases you create, must be stored in this file.
# create file
touch ~/.custom_aliases
Step 2 — Open custom_aliases file

Open the file through a text editor, either through gedit or code (Visual Studio Code), or whatever you prefer.
Visual Studio Code (if installed)
# opens file
code ~/.custom_aliases
Gedit
# opens file
gedit ~/.custom_aliases

Step 3— Create shortcuts (aliases)

Let’s create a simple alias that will print “Welcome John Doe.” when we type welcome in bash terminal.
alias welcome='echo "Welcome $USER."'

Step 4— Update changes

Before you can run the newly created bash command, you must update custom_aliases file.
# update file
source ~/.custom_aliases

Step 5— Execute new bash command

Type the following in your preferred command line.
# command line
welcome
> Welcome John Doe.
Well done! You have now created a custom file for placing aliases, let’s see what types of commands you can create.
Custom aliases for bash commands (personal)

Here are few bash commands I’m using to increase my work-flow.
Nice-Tip: In order to maintain structure when adding many aliases, put them into sections, as shown in example below by using comments.
# Version Control
alias gs="git status"
alias gd="git add ."
alias gp="git push -u origin master"

# Directory
alias diskusage="df -h"
alias folderusage="du -ch"
alias totalfolderusage="du -sh"

# Various
alias opencustomaliases="code ~/.custom_aliases"
alias updatecustomaliases="source ~/.custom_aliases"
alias updatethenupgrade="sudo apt-get update && sudo apt-get upgrade"
Keep in mind that some OS (operating system) may differ, make sure you run these commands in terminal and check if it works before placing them in the custom_aliases file.
Running multiple commands

You can also make one bash command that runs multiple commands. There are two ways you can achieve it, either create a function or create an alias.

Example 1 — Create function

# Multiple commands
function lazyman() {
git add .
git commit -a -m "$1"
git push -u origin master
}

Example 2 — Create alias

# Multiple commands
alias lazyman="git add . && git commit -a -m '$i' && git push -u origin master"
Remember to update the custom_aliases file by running source ~/.custom_aliases, and then type lazyman "First commit".
Thanks to Ani Agajanyan for highlighting the alias shown in example 2 to use && operator between statements instead of ; operator. The && operator will only execute next command if the previous has succeed and so on, while the ; operator doesn’t.

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册帐号

x
回复

使用道具 举报

0

主题

1

帖子

16

积分

Newbie

Rank: 1

积分
16
发表于: 2018-08-05 10:59:50
| 显示全部楼层
开源地址多少呀?
回复

使用道具 举报

3

主题

40

帖子

115

积分

Member

Rank: 2

积分
115
发表于: 2018-08-06 12:12:46
| 显示全部楼层
看不懂也不影响顶贴~~
回复

使用道具 举报

90

主题

205

帖子

697

积分

Moderator

Rank: 7Rank: 7Rank: 7

积分
697
发表于: 2018-08-07 21:59:57
| 显示全部楼层
这个帖子能翻译一下不?感觉很高端啊
每天都怀揣着对未来的憧憬,然后努力奋斗
回复

使用道具 举报

13

主题

97

帖子

276

积分

Full Member

Rank: 3Rank: 3

积分
276
发表于: 2018-08-11 14:01:26
| 显示全部楼层
顶帖
回复

使用道具 举报

90

主题

205

帖子

697

积分

Moderator

Rank: 7Rank: 7Rank: 7

积分
697
发表于: 2018-08-17 00:31:42
| 显示全部楼层
继续顶
每天都怀揣着对未来的憧憬,然后努力奋斗
回复

使用道具 举报

90

主题

205

帖子

697

积分

Moderator

Rank: 7Rank: 7Rank: 7

积分
697
发表于: 2018-08-31 00:43:36
| 显示全部楼层
技术开发还需要大神们来加入,这个版块我是完全看不懂了
每天都怀揣着对未来的憧憬,然后努力奋斗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archive|手机版|小黑屋|palletone.club

GMT+8, 2019-02-23 08:12 , Processed in 0.010443 sec., 12 queries .

Powered by Discuz! X3.4

Release 20180101, © 2001-2019 Comsenz Inc.

MultiLingual version, Rev. 811, © 2009-2019 codersclub.org

快速回复 返回顶部 返回列表