1 |
Hi list! |
2 |
|
3 |
I'm currently streamlining some of my shell scripts to avoid unnecessary |
4 |
process calls where bash itself is powerful enough. |
5 |
|
6 |
At the moment, I want to replace stuff like this: |
7 |
string='foo:bar:foo' |
8 |
second_field=$(echo $string | cut -d : -f 2) # should read "bar" |
9 |
|
10 |
My current solution is using two string operations: |
11 |
string='foo:bar:foo' |
12 |
# remove everything up to and including first ':' |
13 |
second_and_following=${string#*:} |
14 |
# remove everything from the first ':' following |
15 |
second_field=${second_and_following%%:*} |
16 |
|
17 |
Of course, I normally do this in a single line with a subshell but it |
18 |
still looks cumbersome. Is there a way to do it in a single operation |
19 |
without a temporary variable? The following does not work: |
20 |
string='foo:bar:foo' |
21 |
second_field=${string#:%%:*} |
22 |
|
23 |
Thanks in advance! |
24 |
Florian Philipp |