0

I am trying to execute the next code:

P=$(grep -Ei "^export PATH" "$HOME"/.zshrc)
sed '/'"${P}"'/ a Hello_World' $HOME/.zshrc

But i get the next error:

sed '/'"${P}"'/ a Hello_World' $HOME/.zshrc
sed: can't find label for jump to `in/DevTools/flutter/bin/:$HOME/bin/DevTools/Android/Sdk/platform-tools/:/usr/local/go/bin/'

The objective is append the /usr/local/go/bin, with the existing PATH in the user .*rc (bash,zsh,fish).

other thing is i wanna, make this work with variable, because if the user wanna change the location, will do to the Variable instead of the line.

thanks.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Please take a look at [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting). – Cyrus Oct 05 '22 at 19:19
  • shell variables does not expand inside single qoutes. – Jetchisel Oct 05 '22 at 19:20
  • 1
    The problem is that `$P` contains `/` characters and that's ending the `/` that's being used for addressing. – Barmar Oct 05 '22 at 19:57
  • See related: https://stackoverflow.com/questions/5864146/using-different-delimiters-in-sed-commands-and-range-addresses – Barmar Oct 05 '22 at 19:59

2 Answers2

1

The objective is append the /usr/local/go/bin, with the existing PATH in the user .*rc (bash,zsh,fish).

Sooo, do exactly that, for example:

echo 'export PATH="$PATH:/usr/local/go/bin"' >> .zshrc

Doing grep -Ei "^export PATH" is very much not enough, I could have:

: <<COMMENT
export PATH=haha
COMMENT

You can't "parse" user customization files, I can put anything in there. Usually, tools just add the stuff on the end file.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Thanks for the recommendation, although i had thinking this way, but if the user have this, i don't wanna to touch to much the *rcs files of the user. – Walddys E. Dorrejo Céspedes Oct 06 '22 at 12:14
  • Hello, found the way: - awk '/^export PATH/ {$0=$0":'${COMPILE_DIR}'/go"} 1' .bashrc > .bashrc # AWK way to append go path to the existing user "export PATH" - sed "s_^export PATH.*_&:${COMPILE_DIR}/go/bin_" .bashrc # SED way to append go path to the existing user "export PATH" – Walddys E. Dorrejo Céspedes Oct 06 '22 at 12:51
0

Found the way:

- awk '/^export PATH/ {$0=$0":'${COMPILE_DIR}'/go"} 1' $HOME/.bashrc > .bashrc # AWK way to append go path to the existing user "export PATH"
- sed "s_^export PATH.*_&:${COMPILE_DIR}/go/bin_" $HOME/.bashrc # SED way to append go path to the existing user "export PATH"