The Bourne-again Shell (BASH) provides built-in mechanisms for extracting substrings from a string.
You can set the value of a variable with
myvar="some text". You can view the value of the variable
using $myvar or ${myvar}. Putting the "$" in
front of the variable name causes BASH to use the value stored in
myvar.
$ myvar="some text"
$ echo $myvar
some text
$ echo ${myvar}
some text
There is sometimes an advantage to the format that encloses the variable
name in curly braces, i.e. the ${myvar} format.
$ echo "foo$myvar"
foosome text
$ echo "foo$myvarother text"
foo text
$ echo "foo${myvar}other text"
foosome textother text
In the above example, in the instance where the curly braces weren't used,
the value of myvar wasn't displayed, because BASH didn't
know I wanted myvar rather than myvarother, which
has no value assigned to it, so it just dispalyed "foo text". In the second
instance where the curly braces were used, BASH could tell I wanted the
vaue of myvar and displayed "foo some textother text".
Substrings can be extracted from a string by chopping characters from the beginning and end of a string.
Chopping a trailing character:
You can chop a trailing character from a string in BASH by placing the
variable name inside ${ }, such as ${myvar},
and then using %<char>. E.g. suppose myvar
has the value "0064092004008999,". To remove the trailing
comma from the end of the variable, you could use myvar=${myvar%,}
$ myvar="0064092004008999,"
$ echo ${myvar%,}
0064092004008999
If you wanted to remove the last "9" and all characters that appear after it in the line, you can use "*" in the expression.
$ myvar="0064092004008999,"
$ echo ${myvar%9*}
006409200400899
In the example above, the shortest matching substring is selected
and removed, i.e. the "9,". If you wanted to remove the longest
matching substring, e.g. every character from the first "9" onwards,
you could use
$ myvar="0064092004008999,"
$ echo ${myvar%%9*}
00640
Chopping leading characters:
You can chop leading characters from a string by using # or
##.
E.g. suppose myvar has the value
"SNMPv2-MIB::sysContact.0 = STRING: John Smith". If you only
want the name John Smith, you can use ## to
remove the longest substring containing the ":" character.
I.e., using myvar=${myvar##*:} wold work. If you instead used
only one "#", the shortest matching substring would be removed.
I.e., using myvar=${myvar#*:}
would return :sysContact.0 = STRING: John Smith, where all
characters up to and including the first ":" are removed.
$ myvar="SNMPv2-MIB::sysContact.0 = STRING: John Smith"
$ echo ${myvar##*:}
John Smith
$ echo ${myvar#*:}
:sysContact.0 = STRING: John Smith
References:
-
Bash by example, Part 1
Fundamental programming in the Bourne again shell (bash)
Date: March 1, 2000
IBM
