While I was running the command directly via the terminal I was getting the desired output. However when I create a script for the same it isn't working the same.
Basically I was replacing all instances of a string in the file using the sed
command.
I'm new to the bash scripting and I tried to make all possible changes in the script however I wasn't getting the appropriate results.
What are all the things I need to know to avoid such differences? What am I missing here? Why not just simply copying the command from terminal to the script giving the same behavior?
Below is what I run directly via the terminal:
sed -i 's,'"^${var}"','"$change"',gI' ./filename
sed -i 's,'" ${var}"','" $change',gI' ./filename
Here is my script:
change="someotherstring"
while read -r line
do
sed -i 's/'" $line"'/'" $change"'/gI' filename
sed -i 's/'"^$line"'/'"$change"'/gI' filename
done < "$2" #file_from_which_i'm_picking_strings_to_replace
While running the command via the terminal I'm able to replace all the possible target strings.
However, on the script the same command is behaving differently and isn't replacing the possible candidates or rather producing abnormal results.