Score:0

Rename using both captured groups and other variables

ws flag

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/' *
Score:0
hr flag

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)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.