Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
syntax:pe [2017/04/22 03:24] fgrose [Search and replace] |
syntax:pe [2019/11/22 19:03] (current) ersen fix expansion offset and length for substring expansion |
||
---|---|---|---|
Line 78: | Line 78: | ||
__Why does the first one fail?__ It prints nothing, because a parameter (variable) named "''WORDs''" is undefined and thus printed as "" (//nothing//). Without using braces for parameter expansion, Bash will interpret the sequence of all valid characters from the introducing "''$''" up to the last valid character as name of the parameter. When using braces you just force Bash to **only interpret the name inside your braces**. | __Why does the first one fail?__ It prints nothing, because a parameter (variable) named "''WORDs''" is undefined and thus printed as "" (//nothing//). Without using braces for parameter expansion, Bash will interpret the sequence of all valid characters from the introducing "''$''" up to the last valid character as name of the parameter. When using braces you just force Bash to **only interpret the name inside your braces**. | ||
- | Also, please remember, that **parameter names are** (like nearly everything in UNIX(r)) **case sentitive!** | + | Also, please remember, that **parameter names are** (like nearly everything in UNIX(r)) **case sensitive!** |
The second form with the curly braces is also needed to access positional parameters (arguments to a script) beyond ''$9'': | The second form with the curly braces is also needed to access positional parameters (arguments to a script) beyond ''$9'': | ||
Line 192: | Line 192: | ||
* => ''THIS IS SOME TEXT'' | * => ''THIS IS SOME TEXT'' | ||
* ''echo "${array[2]^^}"'' | * ''echo "${array[2]^^}"'' | ||
- | * => ''TEXT'' | + | * => ''SOME'' |
===== Variable name expansion ===== | ===== Variable name expansion ===== | ||
Line 392: | Line 392: | ||
==== Using only Offset ==== | ==== Using only Offset ==== | ||
In the first form, the expansion is used without a length value, note that the offset 0 is the first character: | In the first form, the expansion is used without a length value, note that the offset 0 is the first character: | ||
- | <code>echo ${MYSTRING:34}</code> | + | <code>echo ${MYSTRING:35}</code> |
=> ''<del>Be liberal in what you accept, and </del>conservative in what you send'' | => ''<del>Be liberal in what you accept, and </del>conservative in what you send'' | ||
==== Using Offset and Length ==== | ==== Using Offset and Length ==== | ||
In the second form we also give a length value: | In the second form we also give a length value: | ||
- | <code>echo ${MYSTRING:34:13}</code> | + | <code>echo ${MYSTRING:35:12}</code> |
=> ''<del>Be liberal in what you accept, and </del>conservative<del> in what you send</del>'' | => ''<del>Be liberal in what you accept, and </del>conservative<del> in what you send</del>'' | ||