This shows you the differences between two versions of the page.
|
bash:string_substitutions [2018/08/02 23:32] Rob Stroess created |
— (current) | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ===== bash: "How To" utilize string substitutions ===== | ||
| - | ^ string parameter substitution ^^^^^ | ||
| - | ^operation^what^code^example using:\\ export TST='abcabcaa'^result from\\ abcabcaa^ | ||
| - | |remove|first occurence of substring '__xyz__' in string|STR=${STR/'__xyz__'}|echo ${TST/'bc'}|aabcaa| | ||
| - | |:::|substring '__xyz__' everywhere in string|STR=${STR%%//%%'__xyz__'}|echo ${TST%%//%%'bc'}|aaaa| | ||
| - | |:::|substring '__xyz__' at very beginning of string (prefix)|STR=${STR#'__xyz__'}|echo ${TST#'abca'}|bcaa| | ||
| - | |:::|substring '__xyz__' at very end of string (suffix)|STR=${STR%__xyz__}|echo ${TST%'bcaa'}|abca| | ||
| - | |:::|first __n__ characters|STR=${STR:__n__}|echo ${TST:2}|cabcaa| | ||
| - | |:::|last __n__ characters|STR=${STR:-__n__}|echo ${TST::-3}|abcab| | ||
| - | |extract|all characters after (!) position __n__\\ //(first char position: __n__=0)//|STR=${STR:__n__}|echo ${TST:1}|bcabcaa| | ||
| - | |:::|__m__ characters after (!) position __n__|STR=${STR:__n__:__m__}|echo ${TST:1:5}|bcabc| | ||
| - | |:::|characters after (!) position __n__ until end of string minus __m__|STR=${STR:__n__:-__m__}|echo ${TST:1:-1}|bcabca| | ||
| - | |||