Hi list! I'm currently streamlining some of my shell scripts to avoid unnecessary process calls where bash itself is powerful enough.
At the moment, I want to replace stuff like this:
string='foo:bar:foo'
second_field=$(echo $string | cut -d : -f 2) # should read "bar"
My current solution is using two string operations:
string='foo:bar:foo'
# remove everything up to and including first ':'
second_and_following=${string#*:}
# remove everything from the first ':' following
second_field=${second_and_following%%:*}
Of course, I normally do this in a single line with a subshell but it
still looks cumbersome. Is there a way to do it in a single operation
without a temporary variable? The following does not work:
string='foo:bar:foo'
second_field=${string#:%%:*}
Thanks in advance!
Florian Philipp
signature.asc
Description: OpenPGP digital signature

