programming:bash:expansion
Table of Contents
Variable expansion is a powerful and important Bash feature. It allows for more advanced, convenient manipulation of text without invoking another process. Some expansion types will only work for newer versions of Bash. These limitations are noted where relevant.
Note: Each type of expansion is described in the order that Bash interprets it.
Brace Expansion
- Asterisks (*) do not work! They don't expand until Path Name Expansion, AKA globbing.
- Braces must have a comma somewhere to count as an expansion.
foo{bar,}
→"foobar foo"
andfoo{,bar}
→"foo foobar"
- Supports simple ranges.
{a..m}
→a b c d e f g h i j k l m
- Nests.
{{a..z},{A..Z}}
→a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Bash 4+
- Numeric ranges can be prepended with zeros.
{001..010}
→001 002 003 004 005 006 007 008 009 010
- Iteration step can be defined.
{1..10..2}
→1 3 5 7 9
- Works on letters, too.
{a..m..3}
→a d g j m
Tilde Expansion
"PPAC" Operations
These operations all occur at the same time, in left-to-right order:
Parameter Expansion
Process Substitution
Compatibility depends on OS. It's used to avoid subshells, or use the filesystem as an API.
Arithmetic Expansion
Command Substitution
Word Splitting
Path Name Expansion
References
programming/bash/expansion.txt · Last modified: 2018-09-30 22:27 by zlg