Linux用户经常会使用到Shell,也经常会和控制台打交道。但是这种CLI界面很难被已经习惯使用MS Windows的用户所接受,尤其是命令。Linux常用的Bash命令有一百多个,虽然一般情况下命令都非常简短,不过也有很多时候需要大量的是用参数,这样,对很多初学者来说,背命令变成为最头疼的事情,这也是很多Linux初学者望而生畏。在这里,给大家说一个小技巧,一个可以使大家对控制台的操作更得心应手的技巧——alias(别名)。

  在说这些之前,首先现说一下什么是Shell。我们通常所说的Linux,其实真正含义是特指 LinuxKernel(内核),内核就相当于Linux的核心、大脑,用户的所有操作,都是有Kernel来完成的,但是用户却又不是直接对 Kernel作所有的操作,为什么呢?因为用户与Kernel之间,并不能直接的沟通,用户不可以直接的发送指令给Kernel(不要问我为什么,因为就是这个设计的,呵呵),那用户怎么与Kernel之间联系呢?很简单,这需要一个命令解释器,也就是ShellShellUnixLinux等众多的类似操作中,但扮演这个一个非常重要的角色。他起到了连接用户与内核的功能,所有的用户指令,当输入之后,首先,是有Shell读取,然后解释给内核,由内核来执行。这一点,不同于大家都非常熟悉的MS WindowsDOSShell可以完成对Linux的所有操作,其实大家最长接触的GUI界面,也就是X Window,也是通过图形的方式,完成Shell命令而已。常见的Shell有很多种,在这里举几个例子BashCsh,Ksh等等……我就不一一列举了。我们这里只以Bash为例。

  通过前面的叙述,相信大家已经对Shell有了初步的认识,我们现在具体来说Bash的一个非常好的功能 alias(别名)。顾名思义,别名,就是给一个命令取另外一个名字,他有什么用呢?用处很简单,举一个非常简单的例子:假设说现在有一个命令是 “abcdefgh”,这个命令有点长,如果频繁是用这个命令的话,不免会降低效率。那怎么办呢?我们就给他起个别名,就叫做“123”好了,简短又好记。我们只需要一条命令就可以搞定:

  alias 123='abcdefgh' #注意,这里是单引号

  这样,你就可以用123 来代替这个命令了,而且原来的abcdefgh这条命令依旧有效。这时,相信大家已经知道alias指令的用途了吧。我再举一个例子,如果你总是频繁做一个操作,比如说"cd /home/name/Desktop/",如果你每次要对桌面的文件操作,你就每次都得输入一遍这条命令,有时候,就显的很烦人了。那我们为何不用 alias命令来搞定呢?

  alias zm='cd /home/name/Desktop/'

  这样,我们就可以用zm这个简单的命令取代那一串输入了。不过这里大家要注意,每当你输入一次alias指令后,这个修改只在当前的Shell生效,也就是说,如果你重新开启一个 Shell,或者重新登录之后,这些更改不会保留下来,那如果您希望你的更改是永久的,那怎么做呢?很简单,只需要把你的更改写入bash的配置文件就可以了。你直接修改~/.bashrc文件(/home/username/.bashrc),这个文件是一个隐藏文件。用文本编辑器打开他,比如说vi。然后在其中加入指令就可以了。


  我在最后附了一份我的配置文件,把里面的alias指令我用红色标明了,大家可以参考一下。

  相信大家这时候已经了解alias的用法了。你们是不是会马上会"alias dir='ls'"?呵呵……

  
# ~/.bashrc: executed by bash(1) for non-login shells.

  
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)

  
# for examples

  
# If not running interactively, don't do anything

  
[ -z "$PS1" ] && return

  
# don't put duplicate lines in the history. See bash(1) for more options

  
export HISTCONTROL=ignoredups

  
# check the window size after each command and, if necessary,

  
# update the values of LINES and COLUMNS.

  
shopt -s checkwinsize

  
# make less more friendly for non-text input files, see lesspipe(1)

  
[ -x /usr/bin/lesspipe ] && eval "$(lesspipe)"

  
# set variable identifying the chroot you work in (used in the prompt below)

  
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then

  
debian_chroot=$(cat /etc/debian_chroot)

  
fi

  
# set a fancy prompt (non-color, unless we know we "want" color)

  
case "$TERM" in

  
xterm-color)

  
PS1='${debian_chroot:+($debian_chroot)}/[/033[01;32m/]/u@/h/[/033[00m/]:/[/033[01;34m/]/w/[/033[00m/]/$ '

  
;;

  
*)

  
PS1='${debian_chroot:+($debian_chroot)}/u@/h:/w/$ '

  
;;

  
esac

  
# Comment in the above and uncomment this below for a color prompt

  
#PS1='${debian_chroot:+($debian_chroot)}/[/033[01;32m/]/u@/h/[/033[00m/]:/[/033[01;34m/]/w/[/033[00m/]/$ '

  
# If this is an xterm set the title to user@host:dir

  
case "$TERM" in

  
xterm*|rxvt*)

  
PROMPT_COMMAND='echo -ne "/033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}/007"'

  
;;

  
*)

  
;;

  
esac

  
# Alias definitions.

  
# You may want to put all your additions into a separate file like

  
# ~/.bash_aliases, instead of adding them here directly.

  
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

  
#if [ -f ~/.bash_aliases ]; then

  
# . ~/.bash_aliases

  
#fi

  
# enable color support of ls and also add handy aliases

  
if [ "$TERM" != "dumb" ]; then

  
eval "`dircolors -b`"

  
alias ls='ls --color=auto'

  
#alias dir='ls --color=auto --format=vertical'

  
#alias vdir='ls --color=auto --format=long'

  
fi

  
# some more ls aliases

  
#alias ll='ls -l'

  
#alias la='ls -A'

  
#alias l='ls -CF'

  
alias agi='sudo apt-get install'

  
alias agr='sudo apt-get remove'

  
alias reboot='sudo reboot'

  
alias halt='sudo halt'

  
alias update='sudo apt-get update'

  
alias upgrade='sudo apt-get dist-upgrade'

  
# enable programmable completion features (you don't need to enable

  
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile

  
# sources /etc/bash.bashrc).

  
if [ -f /etc/bash_completion ]; then

  
. /etc/bash_completion

  fi

Logo

更多推荐