如何从shell脚本中的变量中删除回车 [英] How to remove carriage return from a variable in shell script

查看:156
本文介绍了如何从shell脚本中的变量中删除回车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 shell 脚本的新手.我正在使用 source 命令获取一个在 Windows 中创建并带有回车符的文件.在我添加一些字符后,它总是出现在行的开头.

I am new to shell script. I am sourcing a file, which is created in Windows and has carriage returns, using the source command. After I source when I append some characters to it, it always comes to the start of the line.

test.dat(结尾有回车):

testVar=value123

testScript.sh(以上文件来源):

source test.dat
echo $testVar got it

我得到的输出是

got it23

如何从变量中删除'\r'?

推荐答案

另一种解决方案使用 tr:

yet another solution uses tr:

echo $testVar | tr -d '\r'
cat myscript | tr -d '\r'

选项 -d 代表 delete.

这篇关于如何从shell脚本中的变量中删除回车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆