| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | .\" $NetBSD: sh.1,v 1.185 2018/03/13 21:04:57 uwe Exp $ | | 1 | .\" $NetBSD: sh.1,v 1.186 2018/03/13 21:49:15 uwe Exp $ |
2 | .\" Copyright (c) 1991, 1993 | | 2 | .\" Copyright (c) 1991, 1993 |
3 | .\" The Regents of the University of California. All rights reserved. | | 3 | .\" The Regents of the University of California. All rights reserved. |
4 | .\" | | 4 | .\" |
5 | .\" This code is derived from software contributed to Berkeley by | | 5 | .\" This code is derived from software contributed to Berkeley by |
6 | .\" Kenneth Almquist. | | 6 | .\" Kenneth Almquist. |
7 | .\" | | 7 | .\" |
8 | .\" Redistribution and use in source and binary forms, with or without | | 8 | .\" Redistribution and use in source and binary forms, with or without |
9 | .\" modification, are permitted provided that the following conditions | | 9 | .\" modification, are permitted provided that the following conditions |
10 | .\" are met: | | 10 | .\" are met: |
11 | .\" 1. Redistributions of source code must retain the above copyright | | 11 | .\" 1. Redistributions of source code must retain the above copyright |
12 | .\" notice, this list of conditions and the following disclaimer. | | 12 | .\" notice, this list of conditions and the following disclaimer. |
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | | 14 | .\" notice, this list of conditions and the following disclaimer in the |
| @@ -1605,146 +1605,206 @@ or when in an assignment or not in posix | | | @@ -1605,146 +1605,206 @@ or when in an assignment or not in posix |
1605 | or if neither of those appear, the end of the word, | | 1605 | or if neither of those appear, the end of the word, |
1606 | they are treated as a user name | | 1606 | they are treated as a user name |
1607 | and are replaced with the pathname of the named user's home directory. | | 1607 | and are replaced with the pathname of the named user's home directory. |
1608 | If the user name is missing (as in | | 1608 | If the user name is missing (as in |
1609 | .Pa ~/foobar ) , | | 1609 | .Pa ~/foobar ) , |
1610 | the tilde is replaced with the value of the | | 1610 | the tilde is replaced with the value of the |
1611 | .Va HOME | | 1611 | .Va HOME |
1612 | variable (the current user's home directory). | | 1612 | variable (the current user's home directory). |
1613 | .Pp | | 1613 | .Pp |
1614 | In variable assignments, | | 1614 | In variable assignments, |
1615 | an unquoted tilde immediately after the assignment operator (=), and | | 1615 | an unquoted tilde immediately after the assignment operator (=), and |
1616 | each unquoted tilde immediately after an unquoted colon in the value | | 1616 | each unquoted tilde immediately after an unquoted colon in the value |
1617 | to be assigned is also subject to tilde expansion as just stated. | | 1617 | to be assigned is also subject to tilde expansion as just stated. |
| | | 1618 | .\" |
| | | 1619 | .\" |
1618 | .Ss Parameter Expansion | | 1620 | .Ss Parameter Expansion |
| | | 1621 | .\" |
1619 | The format for parameter expansion is as follows: | | 1622 | The format for parameter expansion is as follows: |
1620 | .Pp | | 1623 | .Pp |
1621 | .Dl ${expression} | | 1624 | .Dl ${ Ns Ar expression Ns Li } |
1622 | .Pp | | 1625 | .Pp |
1623 | where expression consists of all characters until the matching | | 1626 | where |
1624 | .Dq } . | | 1627 | .Ar expression |
| | | 1628 | consists of all characters until the matching |
| | | 1629 | .Sq Li } . |
1625 | Any | | 1630 | Any |
1626 | .Dq } | | 1631 | .Sq Li } |
1627 | escaped by a backslash or within a quoted string, and characters in | | 1632 | escaped by a backslash or within a quoted string, and characters in |
1628 | embedded arithmetic expansions, command substitutions, and variable | | 1633 | embedded arithmetic expansions, command substitutions, and variable |
1629 | expansions, are not examined in determining the matching | | 1634 | expansions, are not examined in determining the matching |
1630 | .Dq } . | | 1635 | .Sq Li } . |
1631 | .Pp | | 1636 | .Pp |
1632 | The simplest form for parameter expansion is: | | 1637 | The simplest form for parameter expansion is: |
1633 | .Pp | | 1638 | .Pp |
1634 | .Dl ${parameter} | | 1639 | .Dl ${ Ns Ar parameter Ns Li } |
1635 | .Pp | | 1640 | .Pp |
1636 | The value, if any, of parameter is substituted. | | 1641 | The value, if any, of |
| | | 1642 | .Ar parameter |
| | | 1643 | is substituted. |
1637 | .Pp | | 1644 | .Pp |
1638 | The parameter name or symbol can be enclosed in braces, | | 1645 | The parameter name or symbol can be enclosed in braces, |
1639 | which are optional in this simple case, | | 1646 | which are optional in this simple case, |
1640 | except for positional parameters with more than one digit or | | 1647 | except for positional parameters with more than one digit or |
1641 | when parameter is followed by a character that could be interpreted as | | 1648 | when parameter is followed by a character that could be interpreted as |
1642 | part of the name. | | 1649 | part of the name. |
1643 | If a parameter expansion occurs inside double quotes: | | 1650 | If a parameter expansion occurs inside double quotes: |
1644 | .Bl -enum | | 1651 | .Bl -enum |
1645 | .It | | 1652 | .It |
1646 | Pathname expansion is not performed on the results of the expansion. | | 1653 | pathname expansion is not performed on the results of the expansion; |
1647 | .It | | 1654 | .It |
1648 | Field splitting is not performed on the results of the | | 1655 | field splitting is not performed on the results of the |
1649 | expansion, with the exception of the special rules for @. | | 1656 | expansion, with the exception of the special rules for |
| | | 1657 | .Dv @ . |
1650 | .El | | 1658 | .El |
1651 | .Pp | | 1659 | .Pp |
1652 | In addition, a parameter expansion where braces are used, | | 1660 | In addition, a parameter expansion where braces are used, |
1653 | can be modified by using one of the following formats. | | 1661 | can be modified by using one of the following formats. |
1654 | If the | | 1662 | If the |
1655 | .Dq Dv \&: | | 1663 | .Sq Ic \&: |
1656 | is omitted in the following modifiers, then the test in the expansion | | 1664 | is omitted in the following modifiers, then the test in the expansion |
1657 | applies only to unset parameters, not null ones. | | 1665 | applies only to unset parameters, not null ones. |
1658 | .Bl -tag -width aaparameterwordaaaaa | | 1666 | .Bl -tag -width aaparameterwordaaaaa |
1659 | .It ${parameter:\(miword} | | 1667 | .It Li ${ Ns Ar parameter Ns Ic :- Ns Ar word Ns Li } |
1660 | Use Default Values. | | 1668 | .Sy Use Default Values. |
1661 | If parameter is unset or null, the expansion of word | | 1669 | If |
1662 | is substituted; otherwise, the value of parameter is substituted. | | 1670 | .Ar parameter |
1663 | .It ${parameter:=word} | | 1671 | is unset or null, the expansion of |
1664 | Assign Default Values. | | 1672 | .Ar word |
1665 | If parameter is unset or null, the expansion of | | 1673 | is substituted; otherwise, the value of |
1666 | word is assigned to parameter. | | 1674 | .Ar parameter |
1667 | In all cases, the final value of parameter is substituted. | | 1675 | is substituted. |
| | | 1676 | .It Li ${ Ns Ar parameter Ns Ic := Ns Ar word Ns Li } |
| | | 1677 | .Sy Assign Default Values. |
| | | 1678 | If |
| | | 1679 | .Ar parameter |
| | | 1680 | is unset or null, the expansion of |
| | | 1681 | .Ar word |
| | | 1682 | is assigned to |
| | | 1683 | .Ar parameter . |
| | | 1684 | In all cases, the final value of |
| | | 1685 | .Ar parameter |
| | | 1686 | is substituted. |
1668 | Only variables, not positional parameters or special | | 1687 | Only variables, not positional parameters or special |
1669 | parameters, can be assigned in this way. | | 1688 | parameters, can be assigned in this way. |
1670 | .It ${parameter:?[word]} | | 1689 | .It Li ${ Ns Ar parameter Ns Ic :? Ns Oo Ar word\^ Oc Ns Li } |
1671 | Indicate Error if Null or Unset. | | 1690 | .Sy Indicate Error if Null or Unset. |
1672 | If parameter is unset or null, the | | 1691 | If |
1673 | expansion of word (or a message indicating it is unset if word is omitted) | | 1692 | .Ar parameter |
| | | 1693 | is unset or null, the expansion of |
| | | 1694 | .Ar word |
| | | 1695 | (or a message indicating it is unset if |
| | | 1696 | .Ar word |
| | | 1697 | is omitted) |
1674 | is written to standard error and a non-interactive shell exits with | | 1698 | is written to standard error and a non-interactive shell exits with |
1675 | a nonzero exit status. | | 1699 | a nonzero exit status. |
1676 | An interactive shell will not exit, but any associated command(s) will | | 1700 | An interactive shell will not exit, but any associated command(s) will |
1677 | not be executed. | | 1701 | not be executed. |
1678 | If the parameter is set, its value is substituted. | | 1702 | If the |
1679 | .It ${parameter:+word} | | 1703 | .Ar parameter |
1680 | Use Alternative Value. | | 1704 | is set, its value is substituted. |
1681 | If parameter is unset or null, null is | | 1705 | .It Li ${ Ns Ar parameter Ns Ic :+ Ns Ar word Ns Li } |
1682 | substituted; otherwise, the expansion of word is substituted. | | 1706 | .Sy Use Alternative Value. |
1683 | The value of parameter is not used in this expansion. | | 1707 | If |
1684 | .It ${#parameter} | | 1708 | .Ar parameter |
1685 | String Length. | | 1709 | is unset or null, null is substituted; |
1686 | The length in characters of the value of parameter. | | 1710 | otherwise, the expansion of |
| | | 1711 | .Ar word |
| | | 1712 | is substituted. |
| | | 1713 | The value of |
| | | 1714 | .Ar parameter |
| | | 1715 | .Em is not used |
| | | 1716 | in this expansion. |
| | | 1717 | .It Li ${ Ns Ic # Ns Ar parameter Ns Li } |
| | | 1718 | .Sy String Length. |
| | | 1719 | The length in characters of the value of |
| | | 1720 | .Ar parameter . |
1687 | .El | | 1721 | .El |
1688 | .Pp | | 1722 | .Pp |
1689 | The following four varieties of parameter expansion provide for substring | | 1723 | The following four varieties of parameter expansion provide for substring |
1690 | processing. | | 1724 | processing. |
1691 | In each case, pattern matching notation (see | | 1725 | In each case, pattern matching notation (see |
1692 | .Sx Shell Patterns ) , | | 1726 | .Sx Shell Patterns ) , |
1693 | rather than regular expression notation, is used to evaluate the patterns. | | 1727 | rather than regular expression notation, is used to evaluate the patterns. |
1694 | If parameter is * or @, the result of the expansion is unspecified. | | 1728 | If parameter is |
| | | 1729 | .Dv * |
| | | 1730 | or |
| | | 1731 | .Dv @ , |
| | | 1732 | the result of the expansion is unspecified. |
1695 | Enclosing the full parameter expansion string in double quotes does not | | 1733 | Enclosing the full parameter expansion string in double quotes does not |
1696 | cause the following four varieties of pattern characters to be quoted, | | 1734 | cause the following four varieties of pattern characters to be quoted, |
1697 | whereas quoting characters within the braces has this effect. | | 1735 | whereas quoting characters within the braces has this effect. |
1698 | .Bl -tag -width aaparameterwordaaaaa | | 1736 | .Bl -tag -width aaparameterwordaaaaa |
1699 | .It ${parameter%word} | | 1737 | .It Li ${ Ns Ar parameter Ns Ic % Ns Ar word Ns Li } |
1700 | Remove Smallest Suffix Pattern. | | 1738 | .Sy Remove Smallest Suffix Pattern. |
1701 | The word is expanded to produce a pattern. | | 1739 | The |
1702 | The parameter expansion then results in parameter, with the | | 1740 | .Ar word |
| | | 1741 | is expanded to produce a pattern. |
| | | 1742 | The parameter expansion then results in |
| | | 1743 | .Ar parameter , |
| | | 1744 | with the |
1703 | smallest portion of the suffix matched by the pattern deleted. | | 1745 | smallest portion of the suffix matched by the pattern deleted. |
1704 | If the word is to start with a | | 1746 | If the |
1705 | .Sq \&% | | 1747 | .Ar word |
| | | 1748 | is to start with a |
| | | 1749 | .Sq Li \&% |
1706 | character, it must be quoted. | | 1750 | character, it must be quoted. |
1707 | .It ${parameter%%word} | | 1751 | .It Li ${ Ns Ar parameter Ns Ic %% Ns Ar word Ns Li } |
1708 | Remove Largest Suffix Pattern. | | 1752 | .Sy Remove Largest Suffix Pattern. |
1709 | The word is expanded to produce a pattern. | | 1753 | The |
1710 | The parameter expansion then results in parameter, with the largest | | 1754 | .Ar word |
| | | 1755 | is expanded to produce a pattern. |
| | | 1756 | The parameter expansion then results in |
| | | 1757 | .Ar parameter , |
| | | 1758 | with the largest |
1711 | portion of the suffix matched by the pattern deleted. | | 1759 | portion of the suffix matched by the pattern deleted. |
1712 | The | | 1760 | The |
1713 | .Dq %% | | 1761 | .Dq Ic %% |
1714 | pattern operator only produces different results from the | | 1762 | pattern operator only produces different results from the |
1715 | .Dq \&% | | 1763 | .Dq Ic \&% |
1716 | operator when the pattern contains at least one unquoted | | 1764 | operator when the pattern contains at least one unquoted |
1717 | .Sq \&* . | | 1765 | .Sq Li \&* . |
1718 | .It ${parameter#word} | | 1766 | .It Li ${ Ns Ar parameter Ns Ic \&# Ns Ar word Ns Li } |
1719 | Remove Smallest Prefix Pattern. | | 1767 | .Sy Remove Smallest Prefix Pattern. |
1720 | The word is expanded to produce a pattern. | | 1768 | The |
1721 | The parameter expansion then results in parameter, with the | | 1769 | .Ar word |
| | | 1770 | is expanded to produce a pattern. |
| | | 1771 | The parameter expansion then results in |
| | | 1772 | .Ar parameter , |
| | | 1773 | with the |
1722 | smallest portion of the prefix matched by the pattern deleted. | | 1774 | smallest portion of the prefix matched by the pattern deleted. |
1723 | If the word is to start with a | | 1775 | If the |
1724 | .Sq \&# | | 1776 | .Ar word |
| | | 1777 | is to start with a |
| | | 1778 | .Sq Li \&# |
1725 | character, it must be quoted. | | 1779 | character, it must be quoted. |
1726 | .It ${parameter##word} | | 1780 | .It Li ${ Ns Ar parameter Ns Ic \&## Ns Ar word Ns Li } |
1727 | Remove Largest Prefix Pattern. | | 1781 | .Sy Remove Largest Prefix Pattern. |
1728 | The word is expanded to produce a pattern. | | 1782 | The |
1729 | The parameter expansion then results in parameter, with the largest | | 1783 | .Ar word |
| | | 1784 | is expanded to produce a pattern. |
| | | 1785 | The parameter expansion then results in |
| | | 1786 | .Ar parameter , |
| | | 1787 | with the largest |
1730 | portion of the prefix matched by the pattern deleted. | | 1788 | portion of the prefix matched by the pattern deleted. |
1731 | This has the same relationship with the | | 1789 | This has the same relationship with the |
1732 | .Sq \&# | | 1790 | .Dq Ic \&# |
1733 | pattern operator as | | 1791 | pattern operator as |
1734 | .Dq %% | | 1792 | .Dq Ic %% |
1735 | has with | | 1793 | has with |
1736 | .Dq \&% . | | 1794 | .Dq Ic \&% . |
1737 | .El | | 1795 | .El |
| | | 1796 | .\" |
| | | 1797 | .\" |
1738 | .Ss Command Substitution | | 1798 | .Ss Command Substitution |
1739 | Command substitution allows the output of a command to be substituted in | | 1799 | Command substitution allows the output of a command to be substituted in |
1740 | place of the command (and surrounding syntax). | | 1800 | place of the command (and surrounding syntax). |
1741 | Command substitution occurs when the command is enclosed as follows: | | 1801 | Command substitution occurs when the command is enclosed as follows: |
1742 | .Pp | | 1802 | .Pp |
1743 | .Dl $(command) | | 1803 | .Dl $(command) |
1744 | .Pp | | 1804 | .Pp |
1745 | or | | 1805 | or |
1746 | .Po | | 1806 | .Po |
1747 | .Dq backquoted | | 1807 | .Dq backquoted |
1748 | version | | 1808 | version |
1749 | .Pc : | | 1809 | .Pc : |
1750 | .Pp | | 1810 | .Pp |