我有一個以 bash 結尾的變數\r\n:
$ # Not the real command to get VAR's value, just an example
$ VAR="$(echo -en 'hello\r\n')"
$ hexdump -C <<< "$VAR"
00000000 68 65 6c 6c 6f 0d 0a |hello..|
00000007
我想放棄\r(\nbash 正確處理了它本身)。
我可能會修剪它 ( VAR="$(tr -d '\r' <<< "$VAR")"),但這意味著只為該任務運行一個行程。
我試著用“洗掉匹配后綴模式” bash的功能,但無法找到(例如,其模式使用${VAR%\r},${VAR%\x0d},${VAR%[\r]}-但兩人都不做的作業)。
知道如何在\r不創建子流程的情況下洗掉嗎?
uj5u.com熱心網友回復:
使用 ANSI C 引號。與替換,使用
var=${var//$'\r'}
如果您只想洗掉\rfinal 之前的\n,您可以使用
var=${var%$'\r\n'}$'\n'
即您必須同時洗掉\r和\n,因此您需要添加\n背面。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/358072.html
