Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert bash variable into perl search and replace

Tags:

bash

in my bash script i look for text inside a file and replace it with a variable from my bash script. I couldn't get the variable to be recognized by the perl command. any ideas?

mybash.bash:

#! /bin/sh
let "myNum=2"# myNum could be any integer
perl -pi -e 's/ABCD\d/ABCD$myNum/g' ./textfile.txt

textfile.txt:

'ABCD0' gets replace with 'ABCD' with above script

like image 997
prostock Avatar asked Dec 26 '22 22:12

prostock


1 Answers

Use double quotes so as to enable interpolation of variables:

perl -pi -e "s/ABCD\d/ABCD$myNum/g" ./textfile.txt
           ^^^                   ^^^
like image 123
devnull Avatar answered Jan 09 '23 09:01

devnull