📄 variables2.html
字号:
><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><SPANCLASS="QUOTE">"<TTCLASS="VARNAME">$*</TT>"</SPAN> must be quoted.</P></TD></TR></TABLE></DIV></DD><DT><TTCLASS="VARNAME">$@</TT></DT><DD><P>Same as <SPANCLASS="TOKEN">$*</SPAN>, but each parameter is a quoted string, that is, the parameters are passed on intact, without interpretation or expansion. This means, among other things, that each parameter in the argument list is seen as a separate word.</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>Of course, <SPANCLASS="QUOTE">"<TTCLASS="VARNAME">$@</TT>"</SPAN> should be quoted.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="ARGLIST"></A><P><B>Example 9-6. <BCLASS="COMMAND">arglist</B>: Listing arguments with $* and $@</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # arglist.sh 3 # Invoke this script with several arguments, such as "one two three". 4 5 E_BADARGS=65 6 7 if [ ! -n "$1" ] 8 then 9 echo "Usage: `basename $0` argument1 argument2 etc." 10 exit $E_BADARGS 11 fi 12 13 echo 14 15 index=1 # Initialize count. 16 17 echo "Listing args with \"\$*\":" 18 for arg in "$*" # Doesn't work properly if "$*" isn't quoted. 19 do 20 echo "Arg #$index = $arg" 21 let "index+=1" 22 done # $* sees all arguments as single word. 23 echo "Entire arg list seen as single word." 24 25 echo 26 27 index=1 # Reset count. 28 # What happens if you forget to do this? 29 30 echo "Listing args with \"\$@\":" 31 for arg in "$@" 32 do 33 echo "Arg #$index = $arg" 34 let "index+=1" 35 done # $@ sees arguments as separate words. 36 echo "Arg list seen as separate words." 37 38 echo 39 40 index=1 # Reset count. 41 42 echo "Listing args with \$* (unquoted):" 43 for arg in $* 44 do 45 echo "Arg #$index = $arg" 46 let "index+=1" 47 done # Unquoted $* sees arguments as separate words. 48 echo "Arg list seen as separate words." 49 50 exit 0</PRE></TD></TR></TABLE><HR></DIV><P>Following a <BCLASS="COMMAND">shift</B>, the <TTCLASS="VARNAME">$@</TT> holds the remaining command-line parameters, lacking the previous <TTCLASS="VARNAME">$1</TT>, which was lost. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # Invoke with ./scriptname 1 2 3 4 5 3 4 echo "$@" # 1 2 3 4 5 5 shift 6 echo "$@" # 2 3 4 5 7 shift 8 echo "$@" # 3 4 5 9 10 # Each "shift" loses parameter $1. 11 # "$@" then contains the remaining parameters.</PRE></TD></TR></TABLE> </P><P>The <TTCLASS="VARNAME">$@</TT> special parameter finds use as a tool for filtering input into shell scripts. The <BCLASS="COMMAND">cat "$@"</B> construction accepts input to a script either from <TTCLASS="FILENAME">stdin</TT> or from files given as parameters to the script. See <AHREF="textproc.html#ROT13">Example 12-21</A> and <AHREF="textproc.html#CRYPTOQUOTE">Example 12-22</A>.</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <TTCLASS="VARNAME">$*</TT> and <TTCLASS="VARNAME">$@</TT> parameters sometimes display inconsistent and puzzling behavior, depending on the setting of <AHREF="variables2.html#IFSREF">$IFS</A>.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="INCOMPAT"></A><P><B>Example 9-7. Inconsistent <TTCLASS="VARNAME">$*</TT> and <TTCLASS="VARNAME">$@</TT> behavior</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 # Erratic behavior of the "$*" and "$@" internal Bash variables, 4 #+ depending on whether they are quoted or not. 5 # Inconsistent handling of word splitting and linefeeds. 6 7 8 set -- "First one" "second" "third:one" "" "Fifth: :one" 9 # Setting the script arguments, $1, $2, etc. 10 11 echo 12 13 echo 'IFS unchanged, using "$*"' 14 c=0 15 for i in "$*" # quoted 16 do echo "$((c+=1)): [$i]" # This line remains the same in every instance. 17 # Echo args. 18 done 19 echo --- 20 21 echo 'IFS unchanged, using $*' 22 c=0 23 for i in $* # unquoted 24 do echo "$((c+=1)): [$i]" 25 done 26 echo --- 27 28 echo 'IFS unchanged, using "$@"' 29 c=0 30 for i in "$@" 31 do echo "$((c+=1)): [$i]" 32 done 33 echo --- 34 35 echo 'IFS unchanged, using $@' 36 c=0 37 for i in $@ 38 do echo "$((c+=1)): [$i]" 39 done 40 echo --- 41 42 IFS=: 43 echo 'IFS=":", using "$*"' 44 c=0 45 for i in "$*" 46 do echo "$((c+=1)): [$i]" 47 done 48 echo --- 49 50 echo 'IFS=":", using $*' 51 c=0 52 for i in $* 53 do echo "$((c+=1)): [$i]" 54 done 55 echo --- 56 57 var=$* 58 echo 'IFS=":", using "$var" (var=$*)' 59 c=0 60 for i in "$var" 61 do echo "$((c+=1)): [$i]" 62 done 63 echo --- 64 65 echo 'IFS=":", using $var (var=$*)' 66 c=0 67 for i in $var 68 do echo "$((c+=1)): [$i]" 69 done 70 echo --- 71 72 var="$*" 73 echo 'IFS=":", using $var (var="$*")' 74 c=0 75 for i in $var 76 do echo "$((c+=1)): [$i]" 77 done 78 echo --- 79 80 echo 'IFS=":", using "$var" (var="$*")' 81 c=0 82 for i in "$var" 83 do echo "$((c+=1)): [$i]" 84 done 85 echo --- 86 87 echo 'IFS=":", using "$@"' 88 c=0 89 for i in "$@" 90 do echo "$((c+=1)): [$i]" 91 done 92 echo --- 93 94 echo 'IFS=":", using $@' 95 c=0 96 for i in $@ 97 do echo "$((c+=1)): [$i]" 98 done 99 echo --- 100 101 var=$@ 102 echo 'IFS=":", using $var (var=$@)' 103 c=0 104 for i in $var 105 do echo "$((c+=1)): [$i]" 106 done 107 echo --- 108 109 echo 'IFS=":", using "$var" (var=$@)' 110 c=0 111 for i in "$var" 112 do echo "$((c+=1)): [$i]" 113 done 114 echo --- 115 116 var="$@" 117 echo 'IFS=":", using "$var" (var="$@")' 118 c=0 119 for i in "$var" 120 do echo "$((c+=1)): [$i]" 121 done 122 echo --- 123 124 echo 'IFS=":", using $var (var="$@")' 125 c=0 126 for i in $var 127 do echo "$((c+=1)): [$i]" 128 done 129 130 echo 131 132 # Try this script with ksh or zsh -y. 133 134 exit 0 135 136 # This example script by Stephane Chazelas, 137 # and slightly modified by the document author.</PRE></TD></TR></TABLE><HR></DIV><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>The <BCLASS="COMMAND">$@</B> and <BCLASS="COMMAND">$*</B> parameters differ only when between double quotes.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="IFSEMPTY"></A><P><B>Example 9-8. <TTCLASS="VARNAME">$*</TT> and <TTCLASS="VARNAME">$@</TT> when <TTCLASS="VARNAME">$IFS</TT> is empty</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 # If $IFS set, but empty, 4 #+ then "$*" and "$@" do not echo positional params as expected. 5 6 mecho () # Echo positional parameters. 7 { 8 echo "$1,$2,$3"; 9 } 10 11 12 IFS="" # Set, but empty. 13 set a b c # Positional parameters. 14 15 mecho "$*" # abc,, 16 mecho $* # a,b,c 17 18 mecho $@ # a,b,c 19 mecho "$@" # a,b,c 20 21 # The behavior of $* and $@ when $IFS is empty depends 22 #+ on whatever Bash or sh version being run. 23 # It is therefore inadvisable to depend on this "feature" in a script. 24 25 26 # Thanks, Stephane Chazelas. 27 28 exit 0</PRE></TD></TR></TABLE><HR></DIV></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B>Other Special Parameters</B></P><DL><DT><ANAME="FLPREF"></A><TTCLASS="VARNAME">$-</TT></DT><DD><P>Flags passed to script (using <AHREF="internal.html#SETREF">set</A>). See <AHREF="internal.html#EX34">Example 11-15</A>.</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>This was originally a <ICLASS="EMPHASIS">ksh</I> construct adopted into Bash, and unfortunately it does not seem to work reliably in Bash scripts. One possible use for it is to have a script <AHREF="miscellany.html#IITEST">self-test whether it is interactive</A>.</P></TD></TR></TABLE></DIV></DD><DT><TTCLASS="VARNAME">$!</TT></DT><DD><P>PID (process ID) of last job run in background</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 LOG=$0.log 2 3 COMMAND1="sleep 100" 4 5 echo "Logging PIDs background commands for script: $0" >> "$LOG" 6 # So they can be monitored, and killed as necessary. 7 echo >> "$LOG" 8 9 # Logging commands. 10 11 echo -n "PID of \"$COMMAND1\": " >> "$LOG" 12 ${COMMAND1} & 13 echo $! >> "$LOG" 14 # PID of "sleep 100": 1506 15 16 # Thank you, Jacques Lederer, for suggesting this.</PRE></TD></TR></TABLE> </P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 possibly_hanging_job & { sleep ${TIMEOUT}; eval 'kill -9 $!' &> /dev/null; } 2 # Forces completion of an ill-behaved program. 3 # Useful, for example, in init scripts. 4 5 # Thank you, Sylvain Fourmanoit, for this creative use of the "!" variable.</PRE></TD></TR></TABLE> </P></DD><DT><ANAME="UNDERSCOREREF"></A><TTCLASS="VARNAME">$_</TT></DT><DD><P>Special variable set to last argument of previous command executed.</P><DIVCLASS="EXAMPLE"><HR><ANAME="USCREF"></A><P><B>Example 9-9. Underscore variable</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 echo $_ # /bin/bash 4 # Just called /bin/bash to run the script. 5 6 du >/dev/null # So no output from command. 7 echo $_ # du 8 9 ls -al >/dev/null # So no output from command. 10 echo $_ # -al (last argument) 11 12 : 13 echo $_ # :</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="XSTATVARREF"></A><TTCLASS="VARNAME">$?</TT></DT><DD><P><AHREF="exit-status.html#EXITSTATUSREF">Exit status</A> of a command, <AHREF="functions.html#FUNCTIONREF">function</A>, or the script itself (see <AHREF="functions.html#MAX">Example 23-7</A>)</P></DD><DT><ANAME="PROCCID"></A><TTCLASS="VARNAME">$$</TT></DT><DD><P>Process ID of the script itself. The <TTCLASS="VARNAME">$$</TT> variable often finds use in scripts to construct <SPANCLASS="QUOTE">"unique"</SPAN> temp file names (see <AHREF="contributed-scripts.html#FTPGET">Example A-13</A>, <AHREF="debugging.html#ONLINE">Example 29-6</A>, <AHREF="filearchiv.html#DERPM">Example 12-28</A>, and <AHREF="internal.html#SELFDESTRUCT">Example 11-25</A>). This is usually simpler than invoking <AHREF="filearchiv.html#MKTEMPREF">mktemp</A>.</P></DD></DL></DIV></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN4002"HREF="variables2.html#AEN4002">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>The PID of the currently running script is <TTCLASS="VARNAME">$$</TT>, of course.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN4301"HREF="variables2.html#AEN4301">[2]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>The words <SPANCLASS="QUOTE">"argument"</SPAN> and <SPANCLASS="QUOTE">"parameter"</SPAN> are often used interchangeably. In the context of this document, they have the same precise meaning, that of a variable passed to a script or function.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="part3.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="string-manipulation.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Beyond the Basics</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part3.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Manipulating Strings</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -