2

Given a variable or stdout of a command in bash containing multiple lines, how can I trim leading and trailing whitespace characters (including line breaks)?

Note: I want to use this in trimmed string comparison, i.e.

[[ "$(command1-that-produces-multiline-string)" == "$(command2-that-produces-multiline-string)" ]]

As one of the commands produces trailing newline(s) while the other does not, the comparison fails.

Cybran
  • 2,203
  • 2
  • 17
  • 34
  • 1
    `$(...)` strips trailing newlines, though it does not strip leading newlines or any whitespace. – that other guy May 06 '20 at 17:56
  • 1
    In a slightly similar case [How to trim whitespace from a bash variable](https://stackoverflow.com/questions/369758/) helped me to get it working. – U880D May 06 '20 at 18:25

0 Answers0