0

In this question I understood that we can only refer to captured groups in the rename command using single quotes.

Is there a way to use both captured groups and other variables in the same rename command?

Example:

var="-old_file"
cd /etc
rename 's/(bash\.bashrc)/$1$var/' *

1 Answers1

0

You can either enclose $1 in single quotes, and $var in double quotes:

$ rename -n 's/(bash\.bashrc)/$1'"$var"/ *
rename(bash.bashrc, bash.bashrc-old_file)

or

$ rename -n 's/(bash\.bashrc)/$1'"$var"'/' *
rename(bash.bashrc, bash.bashrc-old_file)

or enclose the whole expression in double quotes, and backslash-escape $1 to prevent it from being expanded by the shell:

$ rename -n "s/(bash\.bashrc)/\$1$var/" *
rename(bash.bashrc, bash.bashrc-old_file)
steeldriver
  • 142,475