| Start |
|---|
| Modifying the case of alphabetic characters To uppercase To lowercase Toggle Case |
Length of parameter Note that it's the length in number of characters which is not necessarily the same as the number of bytes (like in UTF-8 where most characters are encoded in more than one byte), nor the number of glyphs/graphemes (some of which are combinations of characters), nor is it necessarily the same as the display width. |
| Replace pattern in string First match: All matches: Match at the beginning: Match at the end: Replace a pattern with nothing: Add prefix to array items: |
Substrings and subarrays The same expansions apply if the parameter is a positional parameter or the element of a subscripted array: Analogous expansions apply to positional parameters, where offsets are one-based: Substring expansion can be used with indexed arrays: |
| Delete a pattern from the beginning of a string Shortest match: Longest match: |
| Parameter indirection
Some more examples that demonstrate the indirect expansion usage: One more example: |
Default value substitution If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted. If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way. |
| Delete a pattern from the end of a string Shortest match: Longest match: |
| Parameter expansion and filenames You can use Bash Parameter Expansion to emulate common filename-processing operations like We will use this as our example path: To emulate To emulate To emulate |
| Error if variable is empty or unset The semantics for this are similar to that of default value substitution, but instead of substituting a default value, it errors out with the provided error message. The forms are The run the full example above each of the erroring echo statements needs to be commented out to proceed. |
| Munging during expansion Variables don't necessarily have to expand to their values - substrings can be extracted during expansion, which can be useful for extracting file extensions or parts of paths. Globbing characters keep their usual meanings, so It's also possible to expand a variable using a default value - say I want to invoke the user's editor, but if they've not set one I'd like to give them There are two different ways of performing this expansion, which differ in whether the relevant variable is empty or unset. Using Similar to defaults, alternatives can be given; where a default is used if a particular variable isn't available, an alternative is used if the variable is available. Noting that these expansions can be nested, using alternatives becomes particularly useful when supplying arguments to command line flags; Syntax
|
Read Article
https://develop.horje.com/learn/1434/reference