Timeline for In bash: trailing space capture in variable substitution
Current License: CC BY-SA 4.0
9 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Apr 6, 2020 at 7:16 | comment | added | user232326 | Oh, please, In comments there is no space to build a correct answer, I was hopping you could understand what I meant. Having failed, O wrote a more complete and descriptive answer. That should clarify your concerns. | |
Apr 5, 2020 at 20:30 | comment | added | U. Windl |
@Isaac considering your score I'm sure you realized that the XX you are citing is the intermediate step showing the match if no trailing space was found. It's empty then. Your code prints the empty string as final result when the input had no traing space.
|
|
Apr 2, 2020 at 2:07 | comment | added | user232326 |
So, the output from your code of 1,XX is correct, right? Well, then, ${BASH_REMATCH[1]} for my (proposed) code should give exactly the same output, doesn't it?
|
|
Apr 2, 2020 at 1:38 | history | edited | U. Windl | CC BY-SA 4.0 |
Add example with no trailing space
|
Apr 2, 2020 at 1:35 | comment | added | U. Windl |
@Isaac I just checked: My code does not fail, as ${BASH_REMATCH[0]} is empty in that case (no trailing space), and bash handles that without complaints.
|
|
Apr 1, 2020 at 23:14 | comment | added | user232326 |
Use any that you prefer, my example was meant to show that both parts could be captured with one regex, no need for an additional "parameter expansion". Your regex also fails to match if there are no trailing spaces, It seems to me that it is why the exit code $? should be checked, don't you think?
|
|
Apr 1, 2020 at 20:43 | comment | added | U. Windl |
@Isaac Actually I'd prefer [[ "$xxx" =~ (.*[^ ])(\ +$) ]]; printf '<%s>\n' "${BASH_REMATCH[1]}" then. Also your proposal fails if the string in question has no trailing blanks; it would give the empty string then.
|
|
Mar 31, 2020 at 1:07 | comment | added | user232326 |
If you go up to a regex, you might as well capture both parts in only one regex. Please try: [[ "$xxx" =~ (.*[^ ])(\ +$) ]]; printf '<%s>' "${BASH_REMATCH[@]:1}";echo
|
|
Mar 30, 2020 at 13:10 | history | answered | U. Windl | CC BY-SA 4.0 |