📄 othertypesv.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Special Variable Types</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Introduction to Variables and Parameters"HREF="variables.html"><LINKREL="PREVIOUS"TITLE="Bash Variables Are Untyped"HREF="untyped.html"><LINKREL="NEXT"TITLE="Quoting"HREF="quoting.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="untyped.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 4. Introduction to Variables and Parameters</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="quoting.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="OTHERTYPESV"></A>4.4. Special Variable Types</H1><DIVCLASS="VARIABLELIST"><DL><DT><TTCLASS="REPLACEABLE"><I>local variables</I></TT></DT><DD><P>variables visible only within a <AHREF="special-chars.html#CODEBLOCKREF">code block</A> or function (see also <AHREF="localvar.html#LOCALREF">local variables</A> in <AHREF="functions.html#FUNCTIONREF">functions</A>)</P></DD><DT><ANAME="ENVREF"></A><TTCLASS="REPLACEABLE"><I>environmental variables</I></TT></DT><DD><P>variables that affect the behavior of the shell and user interface</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>In a more general context, each <AHREF="special-chars.html#PROCESSREF">process</A> has an <SPANCLASS="QUOTE">"environment"</SPAN>, that is, a group of variables that hold information that the process may reference. In this sense, the shell behaves like any other process.</P><P>Every time a shell starts, it creates shell variables that correspond to its own environmental variables. Updating or adding new environmental variables causes the shell to update its environment, and all the shell's child processes (the commands it executes) inherit this environment.</P></TD></TR></TABLE></DIV><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 space allotted to the environment is limited. Creating too many environmental variables or ones that use up excessive space may cause problems.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>eval "`seq 10000 | sed -e 's/.*/export var&=ZZZZZZZZZZZZZZ/'`"</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>du</B></TT> <TTCLASS="COMPUTEROUTPUT">bash: /usr/bin/du: Argument list too long</TT> </PRE></TD></TR></TABLE> </P><P>(Thank you, St閜hane Chazelas for the clarification, and for providing the above example.)</P></TD></TR></TABLE></DIV><P>If a script sets environmental variables, they need to be <SPANCLASS="QUOTE">"exported"</SPAN>, that is, reported to the environment local to the script. This is the function of the <AHREF="internal.html#EXPORTREF">export</A> command.</P><ANAME="CHILDREF"></A><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>A script can <BCLASS="COMMAND">export</B> variables only to child <AHREF="special-chars.html#PROCESSREF">processes</A>, that is, only to commands or processes which that particular script initiates. A script invoked from the command line <TTCLASS="REPLACEABLE"><I>cannot</I></TT> export variables back to the command line environment. <SPANCLASS="emphasis"><ICLASS="EMPHASIS"><AHREF="internal.html#FORKREF">Child processes</A> cannot export variables back to the parent processes that spawned them.</I></SPAN></P><P>Definition: A <ICLASS="FIRSTTERM">child process</I> is a subprocess launched by another process, its <ICLASS="FIRSTTERM">parent</I>.</P></TD></TR></TABLE></DIV><P>---</P></DD><DT><ANAME="POSPARAMREF1"></A><TTCLASS="REPLACEABLE"><I>positional parameters</I></TT></DT><DD><P>arguments passed to the script from the command line: <TTCLASS="VARNAME">$0</TT>, <TTCLASS="VARNAME">$1</TT>, <TTCLASS="VARNAME">$2</TT>, <TTCLASS="VARNAME">$3</TT> . . .</P><P><ANAME="SCRNAMEPARAM"></A><TTCLASS="VARNAME">$0</TT> is the name of the script itself, <TTCLASS="VARNAME">$1</TT> is the first argument, <TTCLASS="VARNAME">$2</TT> the second, <TTCLASS="VARNAME">$3</TT> the third, and so forth. <ANAME="AEN2193"HREF="#FTN.AEN2193">[1]</A> <ANAME="BRACKETNOTATION"></A> After <TTCLASS="VARNAME">$9</TT>, the arguments must be enclosed in brackets, for example, <TTCLASS="VARNAME">${10}</TT>, <TTCLASS="VARNAME">${11}</TT>, <TTCLASS="VARNAME">${12}</TT>.</P><P>The special variables <AHREF="variables2.html#APPREF">$* and $@</A> denote <SPANCLASS="emphasis"><ICLASS="EMPHASIS">all</I></SPAN> the positional parameters.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX17"></A><P><B>Example 4-5. Positional Parameters</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 # Call this script with at least 10 parameters, for example 4 # ./scriptname 1 2 3 4 5 6 7 8 9 10 5 MINPARAMS=10 6 7 echo 8 9 echo "The name of this script is \"$0\"." 10 # Adds ./ for current directory 11 echo "The name of this script is \"`basename $0`\"." 12 # Strips out path name info (see 'basename') 13 14 echo 15 16 if [ -n "$1" ] # Tested variable is quoted. 17 then 18 echo "Parameter #1 is $1" # Need quotes to escape # 19 fi 20 21 if [ -n "$2" ] 22 then 23 echo "Parameter #2 is $2" 24 fi 25 26 if [ -n "$3" ] 27 then 28 echo "Parameter #3 is $3" 29 fi 30 31 # ... 32 33 34 if [ -n "${10}" ] # Parameters > $9 must be enclosed in {brackets}. 35 then 36 echo "Parameter #10 is ${10}" 37 fi 38 39 echo "-----------------------------------" 40 echo "All the command-line parameters are: "$*"" 41 42 if [ $# -lt "$MINPARAMS" ] 43 then 44 echo 45 echo "This script needs at least $MINPARAMS command-line arguments!" 46 fi 47 48 echo 49 50 exit 0</PRE></TD></TR></TABLE><HR></DIV><P
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -