[英]How to use a shell variable inside sed when doing “d” command?
In my bash, I wish to delete lines from 1 to ${number}, like this: 在我的bash中,我希望将行从1删除为$ {number},如下所示:
number=10
sed '1,${number}d' myfile
It failed with syntax error. 它因语法错误而失败。 I also tried "\\${number}", '"$number"', neithers works
我也尝试过“ \\ $ {number}”,'“ $ number”',但都没有用
I changed to double quote, it works under command line, but not inside bash script file. 我改为双引号,它可以在命令行下运行,但不能在bash脚本文件中运行。 How to make it work?
如何使其运作?
将sed表达式括在双引号内以插入变量number
:
sed "1,${number}d" file
Please use double quotes instead of single quotes while running sed this way. 以这种方式运行sed时,请使用双引号而不是单引号。 Read here for Difference between single and double quotes
在这里阅读单引号和双引号之间的区别
I changed to double quote, it works under command line, but not inside bash script file
我改为双引号,它可以在命令行下运行,但不能在bash脚本文件中使用
Assuming your bash script is: 假设您的bash脚本是:
sed "1,${number}d" myfile
you should export number
variable in bash prompt so that it can be visible in bash script when it is run: 您应该在bash提示符下导出
number
变量,以便在运行时可以在bash脚本中看到它:
export number=10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.