Skip to main content
Included tabs and linefeeds as characters
Source Link
John 9631
  • 557
  • 6
  • 13

I was following similar questions looking for the best strategy to split result strings in bash and dash. My primary strings were window id separated by either spaces or linefeeds. I loved some of the cleverer answers but in the end this boring solution seemed more performant which surprised me after my experience of python loops:

A="Some variable has value abc.123"

for e in $A; do :; done; echo $e

Basically loop over $A doing nothing (:) then echo the last element. If the tabs or linefeeds are expressed with \t and \n you need to express them first so:

for e in $(echo -e $A); do :; done; echo $e

If the separator was a different character, like : say, the translation process eroded any advantage the simple loop provided but with space and "\n"separation it worked well. I'd love to hear if there is something I am missing here.

I was following similar questions looking for the best strategy to split result strings in bash and dash. My primary strings were window id separated by either spaces or linefeeds. I loved some of the cleverer answers but in the end this boring solution seemed more performant which surprised me after my experience of python loops:

A="Some variable has value abc.123"

for e in $A; do :; done; echo $e

If the separator was a different character, like : say, the translation process eroded any advantage the simple loop provided but with space and "\n" it worked well. I'd love to hear if there is something I am missing here.

I was following similar questions looking for the best strategy to split result strings in dash. My primary strings were window id separated by either spaces or linefeeds. I loved some of the cleverer answers but in the end this boring solution seemed more performant which surprised me after my experience of python loops:

A="Some variable has value abc.123"

for e in $A; do :; done; echo $e

Basically loop over $A doing nothing (:) then echo the last element. If the tabs or linefeeds are expressed with \t and \n you need to express them first so:

for e in $(echo -e $A); do :; done; echo $e

If the separator was a different character, like : say, the translation process eroded any advantage the simple loop provided but with space separation it worked well. I'd love to hear if there is something I am missing here.

Source Link
John 9631
  • 557
  • 6
  • 13

I was following similar questions looking for the best strategy to split result strings in bash and dash. My primary strings were window id separated by either spaces or linefeeds. I loved some of the cleverer answers but in the end this boring solution seemed more performant which surprised me after my experience of python loops:

A="Some variable has value abc.123"

for e in $A; do :; done; echo $e

If the separator was a different character, like : say, the translation process eroded any advantage the simple loop provided but with space and "\n" it worked well. I'd love to hear if there is something I am missing here.