8.3 命令补全和别名

时间:2022-04-27
本文章向大家介绍8.3 命令补全和别名,主要内容包括命令补全及别名目录概要、alias别名、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

命令补全及别名目录概要

  • tab键,敲一下,敲两下
  • 参数补全,安装 bash-completion
  • alias别名给命令重新起个名字
  • 各用户都有自己配置别名的文件 ~/.bashrc
  • ls /etc/profile.d/
  • 自定义的alias 放到 ~/.bashrc

命令补全

  • 在centos6中,命令补全仅支持命令本身,参数是不能补全的
  • 在centos7中,支持命令的参数补全
    • 需要安装包bash-completion——>yum install -y bash-completion
    • 安装完成后,需要重启下虚拟机(reboot命令)才可生效

    [root@hf-01 ~]# rpm -qa bash-completion //查看包是否安装完成 bash-completion-2.1-6.el7.noarch

    • 再来测试,会看到一条命令行都可以补全

alias别名

    1. 若是命令较长,可以设置别名
[root@hf-01 ~]# systemctl restart network.service        //重启网络服务
[root@hf-01 ~]# alias restartnet='systemctl restart network.service'
[root@hf-01 ~]# restartnet        //设置别名后,重启网络服务
[root@hf-01 ~]# 
- 取消别名unalias
    - 在取消别名后,在输入别名,就会提示未找到命令
[root@hf-01 profile.d]# unalias restartnet
[root@hf-01 profile.d]# restartnet
-bash: restartnet: 未找到命令
[root@hf-01 profile.d]# 
    1. 直接输入alias 会看到系统中所有的别名(包括自己自定义的alias)
[root@hf-01 ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias restarnet='systemctl restart network.service'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@hf-01 ~]# 
    1. alias存放位置
    • 第一个是存放在用户家目录下的 .bashrc 文件中
    • 第二个是存放在 /etc/profile.d 目录下的 colorls.sh 和 colorgrep.sh 脚本中定义的