there, In my bash script, I define a command line function:
rep() {
find ./ -type f -exec sed -i -e 's/$1/$2/g' {} \;
}
After source ~/.bashrc
when I type: rep get foo
It doesn't work. Does anyone know what's going on here?
there, In my bash script, I define a command line function:
rep() {
find ./ -type f -exec sed -i -e 's/$1/$2/g' {} \;
}
After source ~/.bashrc
when I type: rep get foo
It doesn't work. Does anyone know what's going on here?
Single quotes around the sed 's/$1/$2/g' prevent bash from evaluating the $1 and $2 function arguments into strings - your command right now will swap a file with $1 in it for $2.
Try using double quotes instead.
rep() {
find . -type f -exec sed -i -e "s/$1/$2/g" {} \;
}
$1 because $ in a regex represents the end of line. There can obviously not be anything after end of line, so $1 would never match anything; and so the OP's script would do nothing at all (except needlessly copy the file over itself).