ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzahz_5.4.0.1/paramexp.htm

228 lines
7.2 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content=
"text/html; charset=utf-8">
<title>Parameter expansions</title>
<LINK rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</HEAD>
<body bgcolor="#FFFFFF">
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h2>Parameter expansions</h2>
<p>The format for parameter expansion is as follows:</p>
<p>${<em>expression</em>}</p>
<p>where <em>expression</em> consists of all characters until the
matching right brace (<strong>}</strong>). Any right brace
characters escaped by a backslash or within a string with quotation marks, as
well as characters in embedded arithmetic expansions, command
substitutions, and variable expansions, are not examined in
determining the matching right brace.</p>
<p>The simplest form for parameter expansion is:</p>
<p>${<em>parameter</em>}</p>
<p>The value, if any, of <em>parameter</em> is substituted. The
parameter name or symbol can be enclosed in braces, which are
optional except for positional parameters with more than one digit
or when <em>parameter</em> is followed by a character that might be
interpreted as part of the name. If a parameter expansion occurs
inside double quotation marks, then:</p>
<ol>
<li>Path name expansion is not performed on the results of the
expansion.</li>
<li>Field splitting is not performed on the results of the
expansion, with the exception of <strong>@</strong> special
parameter.</li>
</ol>
<p>A parameter expansion can be modified by using one of the
following formats:</p>
<dl>
<dt><strong>${<em>parameter:-word</em>}</strong></dt>
<dd>Use Default Values. If <em>parameter</em> is unset or null, the
expansion of <em>word</em> is substituted. Otherwise, the value of
<em>parameter</em> is substituted.</dd>
<dt><strong>${<em>parameter:=word</em>}</strong></dt>
<dd>Assign Default Values. If <em>parameter</em> is unset or null,
the expansion of <em>word</em> is assigned to <em>parameter</em>.
In all cases, the final value of <em>parameter</em> is substituted.
Only variables, not positional parameters or special parameters,
can be assigned in this way.</dd>
<dt><strong>${<em>parameter:?word</em>]}</strong></dt>
<dd>Indicate Error if Null or Unset. If <em>parameter</em> is unset
or null, the expansion of <em>word</em> (or a message indicating it
is unset if word is omitted) is written to standard error and a
non-interactive shell exits with a nonzero exit status. Otherwise,
the value of <em>parameter</em> is substituted.</dd>
<dt><strong>${<em>parameter:+word</em>}</strong></dt>
<dd>Use Alternate Value. If <em>parameter</em> is unset or null,
null is substituted. Otherwise, the expansion of <em>word</em> is
substituted.</dd>
</dl>
<p>In the preceding four parameter expansions, using a colon in the
format results in a test for a parameter that is unset or null;
removing the colon results in a test for a parameter that is only
unset.</p>
<dl>
<dt><strong>${#<em>parameter</em>}</strong></dt>
<dd>String Length. If <em>parameter</em> is <strong>@</strong> or
<strong>*</strong>, the number of positional parameters is
substituted. Otherwise, the length of the value of <em>
parameter</em> is substituted.</dd>
<dt><strong>${<em>parameter</em>%<em>word</em>}</strong></dt>
<dd>Remove Smallest Suffix Pattern. The <em>word</em> is expanded
to produce a <a href="pattern.htm">pattern</a>. Then the result is
<em>parameter</em> after removing the smallest portion of the
suffix matched by the pattern.</dd>
<dt><strong>${<em>parameter</em>%%<em>word</em>}</strong></dt>
<dd>Remove Largest Suffix Pattern. The <em>word</em> is expanded to
produce a <a href="pattern.htm">pattern</a>. Then the result is <em>
parameter</em> after removing the largest portion of the suffix
matched by the pattern.</dd>
<dt><strong>${<em>parameter</em>#<em>word</em>}</strong></dt>
<dd>Remove Smallest Prefix Pattern. The <em>word</em> is expanded
to produce a <a href="pattern.htm">pattern</a>. Then the result is
<em>parameter</em> after removing the smallest portion of the
prefix matched by the pattern.</dd>
<dt><strong>${<em>parameter</em>##<em>word</em>}</strong></dt>
<dd>Remove Largest Prefix Pattern. The <em>word</em> is expanded to
produce a <a href="pattern.htm">pattern</a>. Then the result is <em>
parameter</em> after removing the largest portion of the prefix
matched by the pattern.</dd>
<dt>
<strong>${<em>parameter:offset</em>}</strong></dt>
<dt><strong>${<em>parameter:offset:length</em>}</strong></dt>
<dd>Substring Starting at Offset. The value of this expansion is the substring
starting at the byte specified by <em>offset</em> for <em>length</em> bytes.
If <em>length</em> is not
specified or the value of <em>length</em> causes the expansion to exceed the
length of <em>parameter</em>, the substring ends with the last byte of
<em>parameter</em>. Both <em>offset</em> and <em>length</em> are
<a href="arithexp.htm">arithmetic expressions</a> and must evaluate to a value
that is greater than or equal to zero. The first byte of <em>parameter</em> is
defined by an offset of zero.</dd>
<dt><strong>${<em>parameter</em>/<em>pattern</em>/<em>string</em>}</strong></dt>
<dt><strong>${<em>parameter</em>//<em>pattern</em>/<em>string</em>}</strong></dt>
<dd>Substitute String for Pattern. The value of this expansion is the value
of <em>parameter</em> with the longest match of <em>pattern</em> replaced with
<em>string</em>. In the first form, only the first match of <em>pattern</em> is replaced.
In the second form, all matches of <em>pattern</em> are replaced. If <em>pattern</em>
begins with #, it must match at the beginning of <em>parameter</em>. If
<em>pattern</em> begins with a %, it must match at the end of <em>parameter</em>.
</dd>
</dl>
<p><strong>Examples</strong></p>
<ol>
<li>Expand the variable QSH_VERSION.
<pre>
echo ${QSH_VERSION}
</pre>
</li>
<li>Expand the variable filename and use a default value.
<pre>
echo ${filename:-/tmp/default.txt}
</pre>
</li>
<li>Expand the variable index and assign a default value.
<pre>
echo ${index:=0}
</pre>
</li>
<li>Expand the variable filename and indicate an error if unset.
<pre>
echo ${filename:?Variable is not set}
</pre>
</li>
<li>Expand the variable DIRLIST using string length.
<pre>
DIRLIST=/usr/bin:/home/mike
echo ${#DIRLIST}
</pre>
</li>
<li>Expand the variable DIRLIST using remove smallest suffix pattern.
<pre>
DIRLIST=/usr/bin:/home/mike
echo ${DIRLIST%/*}
</pre>
</li>
<li>Expand the variable DIRLIST using remove largest suffix pattern.
<pre>
DIRLIST=/usr/bin:/home/mike
echo ${DIRLIST%%:*}
</pre>
</li>
<li>Expand the variable DIRLIST using remove smallest prefix pattern.
<pre>
DIRLIST=/usr/bin:/home/mike
echo ${DIRLIST#/usr}
</pre>
</li>
<li>Expand the variable DIRLIST using remove largest prefix pattern.
<pre>
DIRLIST=/usr/bin:/home/mike
echo ${DIRLIST##*/}
</pre>
</li>
<li>Expand the variable DIRLIST using a substring starting at offset.
<pre>
DIRLIST=/usr/bin:/home/mike
echo ${DIRLIST:5:3}
</pre>
</li>
<li>Expand the variable DIRLIST using a substitute string for pattern.
<pre>
DIRLIST=/usr/bin:/home/mike
echo ${DIRLIST/m?ke/joel}
</pre>
</li>
</ol>
</body>
</html>