Use double quotes to make the shell expand variables while preserving whitespace: sed -i "s/$var1/ZZ/g" "$file"