I'm writing a bash script to rename files names and using command
rename 's/\d+/sprintf("%02d", $&)/e' *
But would like to replace "%02d" with something like "%0"$var"d" thus replacing '2' with $var but it seems like it doesn't work and I'm not well versed in regex and how to use escape \\ and would like help
Sure, your whole expression is inside single quotes where no expansion is performed. To expand a part of the command, move it out of the quotes:
rename 's/\d+/sprintf("%0'"$var"'d", $&)/e' *
Or put the whole expression in double quotes, but then you'll need to escape more:
rename "s/\\d+/sprintf(\"%0${var}d\", \$&)/e" *
您也可以通过环境传递值
env var=5 rename 's/\d+/sprintf("%0$ENV{var}d", $&)/e' *
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.