Basic Parameters

$VAR ${VAR}

  1. $ foo="foo"
  2. $ echo "$foo"
  3. $ echo "${HOME}/bin/*"
  4. #!/bin/bash
  5. echo "$9"
  6. echo "${11}"

Expansions to Manage Empty Variables

${parameter:-word}

  • If parameter is unset (i.e., does not exist) or is empty, this expansion results in the value of word.
  • If parameter is not empty, the expansion results in the value of parameter. ```bash $ foo= $ echo ${foo:-“substitute value if unset”} substitute value if unset $ echo $foo

$ foo=bar $ echo ${foo:-“substitute value if unset”} bar $ echo $foo bar

<a name="LKn3F"></a>
### `${parameter:=word}`

- If `parameter` is unset or empty, this expansion results in the value of `word`. **
In addition, the value of **`**word**`** is assigned to **`**parameter**`**.** 
- If `parameter` is not empty, the expansion results in the value of `parameter`.
```bash
$ foo=
$ echo ${foo:="default value if unset"}
default value if unset
$ echo $foo
default value if unset

$ foo=bar
$ echo ${foo:="default value if unset"}
bar
$ echo $foo
bar

${parameter:?word}

  • If parameter is unset or empty, this expansion causes the script to exit with an error, and the contents of word are sent to standard error.
  • If parameter is not empty, the expansion results in the value of parameter. ```bash $ foo= $ echo ${foo:?”parameter is empty”} bash: foo: parameter is empty $ echo $? 1

$ foo=bar $ echo ${foo:?”parameter is empty”} bar $ echo $? 0

<a name="aI8z3"></a>
### ${parameter:+word} 

- If `parameter` is unset or empty, the expansion results in nothing. 
- If 
`parameter` is not empty, the value of `word` is substituted for parameter; however, the value of `parameter` is **not **changed.
```bash
$ foo=
$ echo ${foo:+"substitute value if set"}

$ echo $foo

$ foo=bar
$ echo ${foo:+"substitute value if set"}
substitute value if set
$ echo $foo
bar

Expansions That Return Variable Names

${!prefix*} or ${!prefix@}

  • This expansion returns the names of existing variables with names beginning with prefix.
  • According to the bash documentation, both forms of the expansion perform identically.

    $ echo ${!BASH*}
    BASH BASH_ARGC BASH_ARGV BASH_COMMAND BASH_COMPLETION BASH_COMPLETION_DIR 
    BASH_LINENO BASH_SOURCE BASH_SUBSHELL BASH_VERSINFO BASH_VERSION
    

    String Operations

    length

    ${#parameter}

  • expands into the length of the string contained by parameter.

  • $# ${#} ${#*} ${#@} — expand into the number of positional parameters.

    $ foo="This string is long."
    $ echo "'$foo' is ${#foo} characters long."
    'This string is long.' is 20 characters long.
    

    extraction

    ${parameter:offset} ${parameter:offset:length}

  • The extraction begins at offset characters from the beginning of the string and continues until the end of the string, unless length is specified.

  • If the value of offset is negative, it is taken to mean it starts from the end of the string rather than the beginning. Note that negative values must be preceded by a space to prevent confusion with the ${parameter:-word} expansion.
  • length, if present, must not be less than zero.

    $ foo="This string is long."
    $ echo ${foo:5}
    string is long.
    $ echo ${foo:5:6}
    string
    
    $ foo="This string is long."
    $ echo ${foo: -5}
    long.
    $ echo ${foo: -5:2}
    lo
    

    remove leading portion

    ${parameter#pattern} ${parameter##pattern}

  • pattern is a wildcard pattern like those used in pathname expansion.

  • The difference in the two forms is that the # form removes the shortest match, while the ## form removes the longest match.

    $ foo=file.txt.zip
    $ echo ${foo#*.}
    txt.zip
    $ echo ${foo##*.}
    zip
    

    remove ending portion

    ${parameter%pattern} ${parameter%%pattern}

  • They are the same as the previous # and ## expansions, except they remove text from the end of the string contained in parameter rather than from the beginning.

    $ foo=file.txt.zip
    $ echo ${foo%.*}
    file.txt
    $ echo ${foo%%.*}
    file
    

    replace

    ${parameter/pattern/string}
    ${parameter//pattern/string}
    ${parameter/#pattern/string}
    ${parameter/%pattern/string}

  • If text is found matching wildcard pattern, it is replaced with the contents of string.

  • In the normal form, only the first occurrence of pattern is replaced.
  • In the // form, all occurrences are replaced.
  • The /# form requires that the match occur at the beginning of the string,
  • and the /% form requires the match to occur at the end of the string.
  • In every form, /string may be omitted, causing the text matched by pattern to be deleted.
    $ foo=JPG.JPG
    $ echo ${foo/JPG/jpg}
    jpg.JPG
    $ echo ${foo//JPG/jpg}
    jpg.jpg
    $ echo ${foo/#JPG/jpg}
    jpg.JPG
    $ echo ${foo/%JPG/jpg}
    JPG.jpg
    

    Case Conversion

    declare -u declare -l

    #!/bin/bash
    # ul-declare: demonstrate case conversion via declare
    declare -u upper
    declare -l lower
    if [[ $1 ]]; then
          upper="$1"
          lower="$1"
          echo "$upper"
          echo "$lower"
    fi
    
    $ ./ul-declare aBc
    ABC
    abc
    

    Case Conversion Parameter Expansions

    | Format | Result | | —- | —- | | ${parameter,,pattern} | Expand the value of parameter into all lowercase. pattern is an optional shell pattern that will limit which characters (for example, [A-F]) will be converted. See the bash man page for a full description of patterns. | | ${parameter,pattern} | Expand the value of parameter, changing only the first character to lowercase. | | ${parameter^^pattern} | Expand the value of parameter into all uppercase letters. | | ${parameter^pattern} | Expand the value of parameter, changing only the first character to uppercase (capitalization). |
#!/bin/bash
# ul-param: demonstrate case conversion via parameter expansion
if [[ "$1" ]]; then
Strings and Numbers 427
        echo "${1,,}"
        echo "${1,}"
        echo "${1^^}"
        echo "${1^}"
fi
$ ./ul-param aBc
abc
aBc
ABC
ABc