Linux 在 BASH 中,如何从使用 HTML <textarea></textarea> 编写的文件中存在的变量替换 \r

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7800482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:43:35  来源:igfitidea点击:

In BASH, How do i replace \r from a variable that exist in a file written using HTML <textarea></textarea>

linuxbashfedoraarchlinuxbash4

提问by

How do i replace the \r?

我如何替换\r?

#!/bin/bash
...

# setup
if [[ $i =~ $screen ]]; then

    ORIGINAL=${BASH_REMATCH[1]}          # original value is: 3DROTATE\r
    AFTER   =${ORIGINAL/\r/}            # does not replace \r
    myThirdPartyApplication -o $replvar  # FAILS because of \r

fi

采纳答案by imm

You could use sed, i.e.,

你可以使用 sed,即

AFTER=`echo $ORIGINAL | sed 's/\r//g'`

回答by Neil

Just use a literal ^Mcharacter, it has no meaning to bash.

只需使用文字^M字符,它没有任何意义。

回答by tharrrk

This should remove the first \r.

这应该删除第一个 \r。

AFTER="${ORIGINAL/$'\r'/}"

If you need to remove all of them use ${ORIGINAL//$'\r'/}

如果您需要删除所有这些,请使用 ${ORIGINAL//$'\r'/}

回答by 3molo

Another option is to use 'tr' to delete the character, or replace it with \n or anything else.

另一种选择是使用 'tr' 删除该字符,或将其替换为 \n 或其他任何内容。

 ORIGINAL=$(echo ${BASH_REMATCH[1]} | tr -d '\r')

回答by user2510797

Similar to @tharrrk's approach, this parameter substitution also remove the last '\r':

与@tharrrk 的方法类似,此参数替换也删除了最后一个 '\r':

AFTER="${ORIGINAL%'\r'}"