[英]How to add characters to a string between specific characters in bash?
Right now I'm working on creating a script in linux bash shell that adds the word "-BACKUP" to a file name between certain points. 现在,我正在使用linux bash shell创建一个脚本,该脚本在某些点之间的文件名中添加单词“ -BACKUP”。 For example, if I had a file/string called file1.txt I would want to add the "-BACKUP" between "file1" and ".txt" to make "file1-BACKUP.txt". 例如,如果我有一个名为file1.txt的文件/字符串,我想在“ file1”和“ .txt”之间添加“ -BACKUP”,以制作“ file1-BACKUP.txt”。 How would I go about doing that? 我将如何去做? Would I use the basename
command anywhere? 我会在任何地方使用basename
命令吗? In this situation, the extension and stem could be anything, not just what I gave as an example. 在这种情况下,扩展名和词干可以是任何内容,而不仅仅是我作为示例给出的内容。 All help is appreciated! 感谢所有帮助!
Use the substring processing parameter expansion operator %
to remove the suffix from the string, then append the new text. 使用子字符串处理参数扩展运算符%
可以从字符串中删除后缀,然后附加新文本。 The variable must be enclosed in braces for substring processing parameter expansion to work. 变量必须用大括号括起来,子字符串处理参数扩展才能起作用。
var="file1.txt"
echo "${var%.txt}.BACKUP.txt"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.