[英]How to pass an argument with spaces to sed command?
I have the below function in my shell script 我的shell脚本中有以下功能
fnChangeTxt()
{
sed -i 's/<div id="'$1'"><p>*.*</<div id="'$1'"><p>'$2'</' /var/www/html/alarm.html
}
I would like use the below the pass a string to the sed command via $2 argument. 我想在下面使用通过$ 2参数将字符串传递给sed命令。
fnChangeTxt 'demo' 'This text to sed'
Hoever this doesn't work and produces and error, I assume it is due to the white spaces as the below command works fine. 但是,这无法正常工作并产生错误,我认为这是由于空格所致,因为以下命令可以正常工作。
fnChangeTxt 'demo' 'This_text_to_sed'
Is there a way to do this with spaces? 有没有办法用空格做到这一点?
Thanks in advance. 提前致谢。
将参数( $1
等)括在双引号中,以防止它们被单词分割(并防止其他可能不愉快的额外解析):
sed -i "s/<div id=\"$1\"><p>*.*</<div id=\"$1\"><p>$2</" /var/www/html/alarm.html
尝试使用转义字符:
fnChangeTxt 'demo' 'This\ text\ to\ sed'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.