更新时间:2023-12-03 20:48:34
只需使用
while IFS=$' \t\r\n' read -r line
即使每行回车返回字符( \ r
),也将排除前导和尾随空格.无需砍.
It would exclude leading and trailing spaces even carriage returns characters (\r
) every line. No need to chomp it.
如果您仍然想在 \ n
和/或 \ r
之外添加其他空格,则不要指定其他空格:
If you still want to include other spaces besides \n
and/or \r
, just don't specify the others:
while IFS=$'\r\n' read -r line
如果您不喜欢使用IFS,另一种方法是修剪 \ r
:
Another way if you don't like using IFS is just to trim out \r
:
chomped_line=${line%$'\r'}
* -r
防止反斜杠转义任何字符.
* -r
prevents backslashes to escape any characters.