歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> alias——別名

alias——別名

日期:2017/2/27 14:30:16   编辑:更多Linux
  Linux用戶經常會使用到Shell,也經常會和控制台打交道。但是這種CLI界面很難被已經習慣使用MS Windows的用戶所接受,尤其是命令。Linux常用的Bash命令有一百多個,雖然一般情況下命令都非常簡短,不過也有很多時候需要大量的是用參數,這樣,對很多初學者來說,背命令變成為最頭疼的事情,這也是很多Linux初學者望而生畏。在這裡,給大家說一個小技巧,一個可以使大家對控制台的操作更得心應手的技巧——alias(別名)。   在說這些之前,首先現說一下什麼是Shell。我們通常所說的Linux,其實真正含義是特指 Linux的Kernel(內核),內核就相當於Linux的核心、大腦,用戶的所有操作,都是有Kernel來完成的,但是用戶卻又不是直接對 Kernel作所有的操作,為什麼呢?因為用戶與Kernel之間,並不能直接的溝通,用戶不可以直接的發送指令給Kernel(不要問我為什麼,因為就是這個設計的,呵呵),那用戶怎麼與Kernel之間聯系呢?很簡單,這需要一個命令解釋器,也就是Shell。Shell在Unix和Linux等眾多的類似操作中,但扮演這個一個非常重要的角色。他起到了連接用戶與內核的功能,所有的用戶指令,當輸入之後,首先,是有Shell讀取,然後解釋給內核,由內核來執行。這一點,不同於大家都非常熟悉的MS Windows和DOS。Shell可以完成對Linux的所有操作,其實大家最長接觸的GUI界面,也就是X Window,也是通過圖形的方式,完成Shell命令而已。常見的Shell有很多種,在這裡舉幾個例子Bash,Csh,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



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



Copyright © Linux教程網 All Rights Reserved