📄 parameter-substitution.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Parameter Substitution</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Variables Revisited"HREF="variables2.html"><LINKREL="PREVIOUS"TITLE="Manipulating Strings"HREF="string-manipulation.html"><LINKREL="NEXT"TITLE="Typing variables: declare or typeset"HREF="declareref.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="SECT1"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="string-manipulation.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 9. Variables Revisited</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="declareref.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="PARAMETER-SUBSTITUTION"></A>9.3. Parameter Substitution</H1><P><ANAME="PARAMSUBREF"></A></P><DIVCLASS="VARIABLELIST"><P><B><ANAME="PSSUB1"></A>Manipulating and/or expanding variables</B></P><DL><DT><TTCLASS="USERINPUT"><B>${parameter}</B></TT></DT><DD><P>Same as <TTCLASS="REPLACEABLE"><I>$parameter</I></TT>, i.e., value of the variable <TTCLASS="REPLACEABLE"><I>parameter</I></TT>. In certain contexts, only the less ambiguous <TTCLASS="REPLACEABLE"><I>${parameter}</I></TT> form works.</P><P>May be used for concatenating variables with strings.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 your_id=${USER}-on-${HOSTNAME} 2 echo "$your_id" 3 # 4 echo "Old \$PATH = $PATH" 5 PATH=${PATH}:/opt/bin #Add /opt/bin to $PATH for duration of script. 6 echo "New \$PATH = $PATH"</PRE></TD></TR></TABLE></P></DD><DT><ANAME="DEFPARAM1"></A><TTCLASS="USERINPUT"><B>${parameter-default}</B></TT>, <TTCLASS="USERINPUT"><B>${parameter:-default}</B></TT></DT><DD><P>If parameter not set, use default.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 echo ${username-`whoami`} 2 # Echoes the result of `whoami`, if variable $username is still unset.</PRE></TD></TR></TABLE></P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><TTCLASS="REPLACEABLE"><I>${parameter-default}</I></TT> and <TTCLASS="REPLACEABLE"><I>${parameter:-default}</I></TT> are almost equivalent. The extra <SPANCLASS="TOKEN">:</SPAN> makes a difference only when <TTCLASS="PARAMETER"><I>parameter</I></TT> has been declared, but is null. </P></TD></TR></TABLE></DIV><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # param-sub.sh 3 4 # Whether a variable has been declared 5 #+ affects triggering of the default option 6 #+ even if the variable is null. 7 8 username0= 9 echo "username0 has been declared, but is set to null." 10 echo "username0 = ${username0-`whoami`}" 11 # Will not echo. 12 13 echo 14 15 echo username1 has not been declared. 16 echo "username1 = ${username1-`whoami`}" 17 # Will echo. 18 19 username2= 20 echo "username2 has been declared, but is set to null." 21 echo "username2 = ${username2:-`whoami`}" 22 # ^ 23 # Will echo because of :- rather than just - in condition test. 24 # Compare to first instance, above. 25 26 27 # 28 29 # Once again: 30 31 variable= 32 # variable has been declared, but is set to null. 33 34 echo "${variable-0}" # (no output) 35 echo "${variable:-1}" # 1 36 # ^ 37 38 unset variable 39 40 echo "${variable-2}" # 2 41 echo "${variable:-3}" # 3 42 43 exit 0</PRE></TD></TR></TABLE></P><P>The <ICLASS="FIRSTTERM">default parameter</I> construct finds use in providing <SPANCLASS="QUOTE">"missing"</SPAN> command-line arguments in scripts.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 DEFAULT_FILENAME=generic.data 2 filename=${1:-$DEFAULT_FILENAME} 3 # If not otherwise specified, the following command block operates 4 #+ on the file "generic.data". 5 # 6 # Commands follow.</PRE></TD></TR></TABLE> </P><P>See also <AHREF="special-chars.html#EX58">Example 3-4</A>, <AHREF="zeros.html#EX73">Example 28-2</A>, and <AHREF="contributed-scripts.html#COLLATZ">Example A-6</A>.</P><P>Compare this method with <AHREF="list-cons.html#ANDDEFAULT">using an <ICLASS="FIRSTTERM">and list</I> to supply a default command-line argument</A>.</P></DD><DT><TTCLASS="USERINPUT"><B>${parameter=default}</B></TT>, <TTCLASS="USERINPUT"><B>${parameter:=default}</B></TT></DT><DD><P><ANAME="DEFPARAM"></A></P><P>If parameter not set, set it to <ICLASS="FIRSTTERM">default</I>.</P><P>Both forms nearly equivalent. The <SPANCLASS="TOKEN">:</SPAN> makes a difference only when <TTCLASS="VARNAME">$parameter</TT> has been declared and is null, <ANAME="AEN5317"HREF="#FTN.AEN5317">[1]</A> as above. </P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 echo ${username=`whoami`} 2 # Variable "username" is now set to `whoami`.</PRE></TD></TR></TABLE></P></DD><DT><ANAME="PARAMALTV"></A><TTCLASS="USERINPUT"><B>${parameter+alt_value}</B></TT>, <TTCLASS="USERINPUT"><B>${parameter:+alt_value}</B></TT></DT><DD><P>If parameter set, use <TTCLASS="USERINPUT"><B>alt_value</B></TT>, else use null string.</P><P>Both forms nearly equivalent. The <SPANCLASS="TOKEN">:</SPAN> makes a difference only when <TTCLASS="PARAMETER"><I>parameter</I></TT> has been declared and is null, see below.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 echo "###### \${parameter+alt_value} ########" 2 echo 3 4 a=${param1+xyz} 5 echo "a = $a" # a = 6 7 param2= 8 a=${param2+xyz} 9 echo "a = $a" # a = xyz 10 11 param3=123 12 a=${param3+xyz} 13 echo "a = $a" # a = xyz 14 15 echo 16 echo "###### \${parameter:+alt_value} ########" 17 echo 18 19 a=${param4:+xyz} 20 echo "a = $a" # a = 21 22 param5= 23 a=${param5:+xyz} 24 echo "a = $a" # a = 25 # Different result from a=${param5+xyz} 26 27 param6=123 28 a=${param6:+xyz} 29 echo "a = $a" # a = xyz</PRE></TD></TR></TABLE></P></DD><DT><ANAME="QERRMSG"></A><TTCLASS="USERINPUT"><B>${parameter?err_msg}</B></TT>, <TTCLASS="USERINPUT"><B>${parameter:?err_msg}</B></TT></DT><DD><P>If parameter set, use it, else print err_msg.</P><P>Both forms nearly equivalent. The <SPANCLASS="TOKEN">:</SPAN> makes a difference only when <TTCLASS="PARAMETER"><I>parameter</I></TT> has been declared and is null, as above.</P></DD></DL></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX6"></A><P><B>Example 9-16. Using parameter substitution and error messages</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 # Check some of the system's environmental variables. 4 # This is good preventative maintenance. 5 # If, for example, $USER, the name of the person at the console, is not set, 6 #+ the machine will not recognize you. 7 8 : ${HOSTNAME?} ${USER?} ${HOME?} ${MAIL?} 9 echo 10 echo "Name of the machine is $HOSTNAME." 11 echo "You are $USER." 12 echo "Your home directory is $HOME." 13 echo "Your mail INBOX is located in $MAIL." 14 echo 15 echo "If you are reading this message," 16 echo "critical environmental variables have been set." 17 echo 18 echo 19 20 # ------------------------------------------------------ 21 22 # The ${variablename?} construction can also check 23 #+ for variables set within the script. 24 25 ThisVariable=Value-of-ThisVariable 26 # Note, by the way, that string variables may be set 27 #+ to characters disallowed in their names. 28 : ${ThisVariable?} 29 echo "Value of ThisVariable is $ThisVariable". 30 echo 31 echo 32 33 34 : ${ZZXy23AB?"ZZXy23AB has not been set."} 35 # If ZZXy23AB has not been set, 36 #+ then the script terminates with an error message. 37 38 # You can specify the error message. 39 # : ${variablename?"ERROR MESSAGE"} 40 41 42 # Same result with: dummy_variable=${ZZXy23AB?} 43 # dummy_variable=${ZZXy23AB?"ZXy23AB has not been set."} 44 # 45 # echo ${ZZXy23AB?} >/dev/null 46
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -