I am trying to implement sed replace with shell variables. As far for now I have working sed replace with '
sed -i -r 's#(export\ PATH=")(.*)#\1/home/USER/bin:~/.local/bin:\2#' ~/.zshrc
But unfortunately '
does not expand shell variables just as stated in this answer https://stackoverflow.com/a/5156322/675100
While trying to use "
I keep getting problems with parentheses etc. I would like to make this generic to always use whoami
instead of USER
The first attempt would be
sed -i -r "s#\(export\ PATH=\"\)(.*)#\1/home/`whoami`/bin:~/.local/bin:\2#" ~/.zshrc
sed: -e expression #1, char 60: invalid reference \2 on `s' command's RHS
EDIT
I have tried
sed -i -r "s#\(export\ PATH=\"\)\(.*\)#\1/home/$(whoami)/bin:~/.local/bin:\2#" ~/.zshrc
to escape the second group but I get the similar error :
sed: -e expression #1, char 63: invalid reference \2 on `s' command's RHS