old-www/LDP/abs/html/options.html

948 lines
12 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Options</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="Advanced Topics"
HREF="part5.html"><LINK
REL="PREVIOUS"
TITLE="Debugging"
HREF="debugging.html"><LINK
REL="NEXT"
TITLE="Gotchas"
HREF="gotchas.html"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: </TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="debugging.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="gotchas.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="OPTIONS"
></A
>Chapter 33. Options</H1
><P
><A
NAME="OPTIONSREF"
></A
></P
><P
>Options are settings that change shell and/or script
behavior.</P
><P
>The <A
HREF="internal.html#SETREF"
>set</A
> command
enables options within a script. At the point in the script
where you want the options to take effect, use <B
CLASS="COMMAND"
>set
-o option-name</B
> or, in short form, <B
CLASS="COMMAND"
>set
-option-abbrev</B
>. These two forms are equivalent.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
> #!/bin/bash
set -o verbose
# Echoes all commands before executing.
</PRE
></FONT
></TD
></TR
></TABLE
></P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
> #!/bin/bash
set -v
# Exact same effect as above.
</PRE
></FONT
></TD
></TR
></TABLE
></P
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>To <I
CLASS="FIRSTTERM"
>disable</I
> an option within a script,
use <B
CLASS="COMMAND"
>set +o option-name</B
> or <B
CLASS="COMMAND"
>set
+option-abbrev</B
>.</P
></TD
></TR
></TABLE
></DIV
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
> #!/bin/bash
set -o verbose
# Command echoing on.
command
...
command
set +o verbose
# Command echoing off.
command
# Not echoed.
set -v
# Command echoing on.
command
...
command
set +v
# Command echoing off.
command
exit 0
</PRE
></FONT
></TD
></TR
></TABLE
></P
><P
>An alternate method of enabling options in a script is
to specify them immediately following the
<TT
CLASS="REPLACEABLE"
><I
>#!</I
></TT
> script header.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
> #!/bin/bash -x
#
# Body of script follows.
</PRE
></FONT
></TD
></TR
></TABLE
></P
><P
><A
NAME="INVOCATIONOPTIONSREF"
></A
></P
><P
>It is also possible to enable script options from the command
line. Some options that will not work with
<B
CLASS="COMMAND"
>set</B
> are available this way. Among these
are <TT
CLASS="REPLACEABLE"
><I
>-i</I
></TT
>, force script to run
interactive.</P
><P
><TT
CLASS="USERINPUT"
><B
>bash -v script-name</B
></TT
></P
><P
><TT
CLASS="USERINPUT"
><B
>bash -o verbose script-name</B
></TT
></P
><P
>The following is a listing of some useful options. They may be
specified in either abbreviated form (preceded by a single dash)
or by complete name (preceded by a <EM
>double</EM
>
dash or by <TT
CLASS="OPTION"
>-o</TT
>).</P
><P
><A
NAME="OPTIONSTABLE"
></A
></P
><DIV
CLASS="TABLE"
><A
NAME="AEN19601"
></A
><P
><B
>Table 33-1. Bash options</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
ALIGN="LEFT"
VALIGN="TOP"
>Abbreviation</TH
><TH
ALIGN="LEFT"
VALIGN="TOP"
>Name</TH
><TH
ALIGN="LEFT"
VALIGN="TOP"
>Effect</TH
></TR
></THEAD
><TBODY
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-B</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>brace expansion</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><EM
>Enable</EM
>
<A
HREF="special-chars.html#BRACEEXPREF"
>brace
expansion</A
> (default setting =
<EM
>on</EM
>)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>+B</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>brace expansion</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><EM
>Disable</EM
>
brace expansion</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><A
NAME="NOCLOBBERREF"
></A
><TT
CLASS="OPTION"
>-C</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>noclobber</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Prevent overwriting of files by redirection (may be
overridden by <SPAN
CLASS="TOKEN"
>&#62;|</SPAN
>)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-D</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>(none)</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>List double-quoted strings prefixed by <SPAN
CLASS="TOKEN"
>$</SPAN
>,
but do not execute commands in script</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-a</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>allexport</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Export all defined variables</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-b</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>notify</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Notify when jobs running in background terminate (not of
much use in a script)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-c ...</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>(none)</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Read commands from <B
CLASS="COMMAND"
>...</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>checkjobs</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Informs user of any open <A
HREF="x9644.html#JOBSREF"
>jobs</A
> upon shell exit.
Introduced in <A
HREF="bashver4.html#BASH4REF"
>version 4</A
>
of Bash, and still <SPAN
CLASS="QUOTE"
>"experimental."</SPAN
>
<EM
>Usage:</EM
> shopt -s checkjobs
(<EM
>Caution:</EM
> may hang!)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-e</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>errexit</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Abort script at first error, when a command
exits with non-zero status (except in <A
HREF="loops1.html#UNTILLOOPREF"
>until</A
> or <A
HREF="loops1.html#WHILELOOPREF"
>while loops</A
>, <A
HREF="testconstructs.html#TESTCONSTRUCTS1"
>if-tests</A
>, <A
HREF="list-cons.html#LCONS1"
>list constructs</A
>)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-f</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>noglob</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Filename expansion (globbing) disabled</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>globstar</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><A
HREF="bashver4.html#GLOBSTARREF"
><I
CLASS="FIRSTTERM"
>globbing</I
>
star-match</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Enables the <SPAN
CLASS="TOKEN"
>**</SPAN
> <A
HREF="globbingref.html"
>globbing</A
> operator
(<A
HREF="bashver4.html#BASH4REF"
>version 4+</A
> of Bash).
<EM
>Usage:</EM
> shopt -s globstar</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-i</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>interactive</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Script runs in <I
CLASS="FIRSTTERM"
>interactive</I
> mode</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-n</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>noexec</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Read commands in script, but do not execute them (syntax check)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-o Option-Name</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>(none)</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Invoke the <I
CLASS="FIRSTTERM"
>Option-Name</I
>
option</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-o posix</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>POSIX</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Change the behavior of Bash, or invoked script, to
conform to <A
HREF="sha-bang.html#POSIX2REF"
>POSIX</A
>
standard.</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-o pipefail</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>pipe failure</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Causes a pipeline to return the <A
HREF="exit-status.html#EXITSTATUSREF"
>exit status</A
> of
the last command in the pipe that returned a non-zero
return value.</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-p</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>privileged</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Script runs as <SPAN
CLASS="QUOTE"
>"suid"</SPAN
> (caution!)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-r</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>restricted</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Script runs in <I
CLASS="FIRSTTERM"
>restricted</I
>
mode (see <A
HREF="restricted-sh.html"
>Chapter 22</A
>).</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-s</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>stdin</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Read commands from <TT
CLASS="FILENAME"
>stdin</TT
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-t</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>(none)</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Exit after first command</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-u</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>nounset</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Attempt to use undefined variable
outputs error message, and forces an exit</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-v</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>verbose</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Print each command to <TT
CLASS="FILENAME"
>stdout</TT
> before executing it</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-x</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>xtrace</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Similar to <TT
CLASS="OPTION"
>-v</TT
>, but expands commands</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>-</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>(none)</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>End of options flag. All other arguments
are <A
HREF="internalvariables.html#POSPARAMREF"
>positional
parameters</A
>.</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>--</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>(none)</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Unset positional parameters.
If arguments given (<TT
CLASS="PARAMETER"
><I
>-- arg1 arg2</I
></TT
>),
positional parameters set to arguments.</TD
></TR
></TBODY
></TABLE
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="debugging.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="gotchas.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Debugging</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="part5.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Gotchas</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>