⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aliases.html

📁 Shall高级编程
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Aliases</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Advanced Topics"HREF="part5.html"><LINKREL="PREVIOUS"TITLE="Recursion Without Local Variables"HREF="recurnolocvar.html"><LINKREL="NEXT"TITLE="List Constructs"HREF="list-cons.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="recurnolocvar.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="list-cons.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="ALIASES"></A>Chapter 24. Aliases</H1><P><ANAME="ALIASREF"></A></P><P>A Bash <ICLASS="FIRSTTERM">alias</I> is essentially nothing	more than a keyboard shortcut, an abbreviation, a means of	avoiding typing a long command sequence. If, for example,	we include <BCLASS="COMMAND">alias lm="ls -l | more"</B> in	the <AHREF="files.html#FILESREF1"><TTCLASS="FILENAME">~/.bashrc</TT>	file</A>, then each <TTCLASS="USERINPUT"><B>lm</B></TT> typed at the	command line will automatically be replaced by a <BCLASS="COMMAND">ls -l	| more</B>. This can save a great deal of typing at the	command line and avoid having to remember complex combinations of	commands and options. Setting <BCLASS="COMMAND">alias rm="rm -i"</B>	(interactive mode delete) may save a good deal of grief, since	it can prevent inadvertently losing important files.</P><P>In a script, aliases have very limited usefulness. It would be	quite nice if aliases could assume some of the functionality of	the C preprocessor, such as macro expansion, but unfortunately	Bash does not expand arguments within the alias body.	  <ANAME="AEN17194"HREF="#FTN.AEN17194">[1]</A>	Moreover, a script fails to expand an alias itself	within <SPANCLASS="QUOTE">"compound constructs"</SPAN>, such as <AHREF="tests.html#IFTHEN">if/then</A> statements, loops, and	functions. An added limitation is that an alias will not expand	recursively. Almost invariably, whatever we would like an alias	to do could be accomplished much more effectively with a <AHREF="functions.html#FUNCTIONREF">function</A>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="AL"></A><P><B>Example 24-1. Aliases within a script</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# alias.sh   3&nbsp;   4&nbsp;shopt -s expand_aliases   5&nbsp;# Must set this option, else script will not expand aliases.   6&nbsp;   7&nbsp;   8&nbsp;# First, some fun.   9&nbsp;alias Jesse_James='echo "\"Alias Jesse James\" was a 1959 comedy starring Bob Hope."'  10&nbsp;Jesse_James  11&nbsp;  12&nbsp;echo; echo; echo;  13&nbsp;  14&nbsp;alias ll="ls -l"  15&nbsp;# May use either single (') or double (") quotes to define an alias.  16&nbsp;  17&nbsp;echo "Trying aliased \"ll\":"  18&nbsp;ll /usr/X11R6/bin/mk*   #* Alias works.  19&nbsp;  20&nbsp;echo  21&nbsp;  22&nbsp;directory=/usr/X11R6/bin/  23&nbsp;prefix=mk*  # See if wild card causes problems.  24&nbsp;echo "Variables \"directory\" + \"prefix\" = $directory$prefix"  25&nbsp;echo  26&nbsp;  27&nbsp;alias lll="ls -l $directory$prefix"  28&nbsp;  29&nbsp;echo "Trying aliased \"lll\":"  30&nbsp;lll         # Long listing of all files in /usr/X11R6/bin stating with mk.  31&nbsp;# An alias can handle concatenated variables -- including wild card -- o.k.  32&nbsp;  33&nbsp;  34&nbsp;  35&nbsp;  36&nbsp;TRUE=1  37&nbsp;  38&nbsp;echo  39&nbsp;  40&nbsp;if [ TRUE ]  41&nbsp;then  42&nbsp;  alias rr="ls -l"  43&nbsp;  echo "Trying aliased \"rr\" within if/then statement:"  44&nbsp;  rr /usr/X11R6/bin/mk*   #* Error message results!  45&nbsp;  # Aliases not expanded within compound statements.  46&nbsp;  echo "However, previously expanded alias still recognized:"  47&nbsp;  ll /usr/X11R6/bin/mk*  48&nbsp;fi    49&nbsp;  50&nbsp;echo  51&nbsp;  52&nbsp;count=0  53&nbsp;while [ $count -lt 3 ]  54&nbsp;do  55&nbsp;  alias rrr="ls -l"  56&nbsp;  echo "Trying aliased \"rrr\" within \"while\" loop:"  57&nbsp;  rrr /usr/X11R6/bin/mk*   #* Alias will not expand here either.  58&nbsp;                           #  alias.sh: line 57: rrr: command not found  59&nbsp;  let count+=1  60&nbsp;done   61&nbsp;  62&nbsp;echo; echo  63&nbsp;  64&nbsp;alias xyz='cat $0'   # Script lists itself.  65&nbsp;                     # Note strong quotes.  66&nbsp;xyz  67&nbsp;#  This seems to work,  68&nbsp;#+ although the Bash documentation suggests that it shouldn't.  69&nbsp;#  70&nbsp;#  However, as Steve Jacobson points out,  71&nbsp;#+ the "$0" parameter expands immediately upon declaration of the alias.  72&nbsp;  73&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="UNALIASREF"></A></P><P>The <BCLASS="COMMAND">unalias</B> command removes a previously      set <ICLASS="FIRSTTERM">alias</I>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="UNAL"></A><P><B>Example 24-2. <ICLASS="FIRSTTERM">unalias</I>: Setting and unsetting      an alias</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# unalias.sh   3&nbsp;   4&nbsp;shopt -s expand_aliases  # Enables alias expansion.   5&nbsp;   6&nbsp;alias llm='ls -al | more'   7&nbsp;llm   8&nbsp;   9&nbsp;echo  10&nbsp;  11&nbsp;unalias llm              # Unset alias.  12&nbsp;llm  13&nbsp;# Error message results, since 'llm' no longer recognized.  14&nbsp;  15&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>./unalias.sh</B></TT> <TTCLASS="COMPUTEROUTPUT">total 6drwxrwxr-x    2 bozo     bozo         3072 Feb  6 14:04 .drwxr-xr-x   40 bozo     bozo         2048 Feb  6 14:04 ..-rwxr-xr-x    1 bozo     bozo          199 Feb  6 14:04 unalias.sh./unalias.sh: llm: command not found</TT></PRE></TD></TR></TABLE></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN17194"HREF="aliases.html#AEN17194">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>However, aliases do seem to expand positional	  parameters.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="recurnolocvar.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="list-cons.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Recursion Without Local Variables</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part5.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">List Constructs</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -