How to remove extra spaces in bash?

后端 未结 10 1734
说谎
说谎 2020-12-01 05:46

How to remove extra spaces in variable HEAD?

HEAD=\"    how to  remove    extra        spaces                     \"

Result:

10条回答
  •  伪装坚强ぢ
    2020-12-01 06:12

    Try this one:

    echo '    how to  remove    extra        spaces                     ' | sed 's/^ *//g' | sed 's/$ *//g' | sed 's/   */ /g'
    

    or

    HEAD="    how to  remove    extra        spaces                     "
    HEAD=$(echo "$HEAD" | sed 's/^ *//g' | sed 's/$ *//g' | sed 's/   */ /g')
    

提交回复
热议问题