Linux alias 实现命令别名


目录
  • 1 Alias 别名功能介绍
  • 2 配置方法
    • 一直生效
    • 临时生效
  • 3 常用命令
    • 3.1 查看帮助
    • 3.2 查看已有别名
    • 3.3 添加、修改和去除别名
  • 案例解析:Linux系统安装数据库管理工具XAMPP,如何通过命令行打开 mysql

1 Alias 别名功能介绍

主要功能作用:简化频繁使用但冗长复杂的命令的输入

  • 进入常用的工作目录,层级很深,每次都需要使用 cd 进入
    alias myworkdir='cd /home/username/desktop/myworkdir'
    
  • 简化的命令输入,比如通过c alias -p 命令查看定义好的别名,
    alias ll='ls -alF' ,# ll是对 ls -alF 的别名
    
  • 打开不同版本的python和pip,除了添加软链接,还可以通过别名的方式
    alias mypip3='/home/work/softwares/Python-3.7.7/Output/bin/pip3.7'
    alias mypython3='/home/work/softwares/Python-3.7.7/Output/bin/python3.7'
    

2 配置方法

一直生效

vim ~/.bashrc # 用vim打开配置文件,在 alias 位置添加别名(其他位置也可以添加,放一起会整洁些)

source ~/.bashrc # 编辑完可以用source命令刷新

临时生效

在命令行中操作,可用于当前登录环境,退出登录失效

3 常用命令

语法:alias [-p] [name[=value] ... ] 注意‘=’和字符串之间不能包含空格

3.1 查看帮助

?? Desktop  15:16 alias --help
alias: alias [-p] [name[=value] ... ]
    Define or display aliases.
    
    Without arguments, `alias' prints the list of aliases in the reusable
    form `alias NAME=VALUE' on standard output.
    
    Otherwise, an alias is defined for each NAME whose VALUE is given.
    A trailing space in VALUE causes the next word to be checked for
    alias substitution when the alias is expanded.
    
    Options:
      -p	print all defined aliases in a reusable format
    
    Exit Status:
    alias returns true unless a NAME is supplied for which no alias has been
    defined.

3.2 查看已有别名

?? Desktop  ? 15:17 alias -p
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

?? Desktop  ? 15:18 alias ll
alias ll='ls -alF'

3.3 添加、修改和去除别名

// 添加别名:返回上一级并查看文件,多个命令,用 “;” 或者 “&&” 连接
?? bin  ? 16:11 alias '..ll=cd ..;ll'
// 查看别名,发现已存在
?? bin  16:11 alias -p
alias ..ll='cd ..;ll'
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias mysql_login='sudo /opt/lampp/lampp start && cd /opt/lampp/bin/ && ./mysql -u root'
alias mysql_logout='sudo /opt/lampp/lampp stop'
//修改别名
?? lampp  16:15 alias '..ll=cd ~/Desktop/ ; ll'
?? lampp  16:17 alia -p

Command 'alia' not found, did you mean:

  command 'ali' from deb mailutils-mh (1:3.7-2.1)
  command 'ali' from deb mmh (0.4-2)
  command 'ali' from deb nmh (1.7.1-6)
  command 'alsa' from deb alsa-base (1.0.25+dfsg-0ubuntu5)
  command 'alfa' from deb alfa (1.0-3build2)

Try: sudo apt install 
// 查看别名,发现已修改
?? lampp  ? 16:17 alias -p
alias ..ll='cd ~/Desktop/ ; ll'
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias mysql_login='sudo /opt/lampp/lampp start && cd /opt/lampp/bin/ && ./mysql -u root'
alias mysql_logout='sudo /opt/lampp/lampp stop'
// 去除别名
?? lampp  16:12 unalias ..ll
// 查看别名,发现已去除
?? lampp  16:12 alias -p
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias mysql_login='sudo /opt/lampp/lampp start && cd /opt/lampp/bin/ && ./mysql -u root'
alias mysql_logout='sudo /opt/lampp/lampp stop'

案例解析:Linux系统安装数据库管理工具XAMPP,如何通过命令行打开 mysql

XAMPP 安装参考这里,安装后,mysql数据库后启动,进入 /opt/lampp/bin,执行mysql -u root,方可启动。当然可以通过添加环境变量的方式,简化路径切换,但是两个命令还是带来了些麻烦,下面演示添加别名方式来实现“mysql-login”,“mysql-logout”, 结合别名使用,终端还会有提示,更方便了使用。

// 添加mysql_login
?? Desktop  ? 15:47 alias mysql_login="sudo /opt/lampp/lampp start && cd /opt/lampp/bin/ ./mysql -u root"

?? Desktop  16:05 alias mysql_logout="sudo /opt/lampp/lampp stop"
?? Desktop  16:05 mysql_login
[sudo] password for yuanchengwei: 
Starting XAMPP for Linux 7.4.28-1...
XAMPP: Starting Apache.../opt/lampp/share/xampp/xampplib: line 22: netstat: command not found
/opt/lampp/share/xampp/xampplib: line 22: netstat: command not found
ok.
XAMPP: Starting MySQL.../opt/lampp/share/xampp/xampplib: line 22: netstat: command not found
ok.
XAMPP: Starting ProFTPD.../opt/lampp/share/xampp/xampplib: line 22: netstat: command not found
ok.
bash: cd: too many arguments
// 添加失败,修改mysql_login
?? Desktop  ? 16:06 alias mysql_login="sudo /opt/lampp/lampp start && cd /opt/lampp/bin/ && ./mysql -u root"
// 查看是否添加成功
?? Desktop  16:09 alias -p
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias mysql_login='sudo /opt/lampp/lampp start && cd /opt/lampp/bin/ && ./mysql -u root'
alias mysql_logout='sudo /opt/lampp/lampp stop'
?? Desktop  16:09 mysql_logout
Stopping XAMPP for Linux 7.4.28-1...
XAMPP: Stopping Apache...ok.
XAMPP: Stopping MySQL...ok.
XAMPP: Stopping ProFTPD...ok.
?? Desktop  16:09 alias mysql_login="sudo /opt/lampp/lampp start && cd /opt/lampp/bin/ && ./mysql -u root"
?? Desktop  16:09 mysql_login
Starting XAMPP for Linux 7.4.28-1...
XAMPP: Starting Apache.../opt/lampp/share/xampp/xampplib: line 22: netstat: command not found
/opt/lampp/share/xampp/xampplib: line 22: netstat: command not found
ok.
XAMPP: Starting MySQL.../opt/lampp/share/xampp/xampplib: line 22: netstat: command not found
ok.
XAMPP: Starting ProFTPD.../opt/lampp/share/xampp/xampplib: line 22: netstat: command not found
ok.
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.4.24-MariaDB Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit
Bye

End