-1

I want to write a line mac .bash_profile file while executing a shell script.I have tried below way but it gives error as

 sed -i 'export PATH='$PWD:'$PATH' $HOME/.bash_profile

error

sed: 1: "/Users/dhiraj/.bash_pro ...": extra characters at the end of d command

Kindly let me know how do i resolve it ?

TechChain
  • 8,404
  • 29
  • 103
  • 228
  • 1
    You are using the `'i'` wrong (currently you are giving the `"edit in-place"` option, not the `/i` (insert before) command. – David C. Rankin Feb 04 '19 at 08:38
  • Duplicate of https://stackoverflow.com/questions/4247068/sed-command-with-i-option-failing-on-mac-but-works-on-linux – Biffen Feb 04 '19 at 11:50

1 Answers1

2

If you just want to append a line to your .bash_profile, what about

echo "PATH=\"$PWD:$PATH\"" >> $HOME/.bash_profile

?

And, if you want to be sure to insert only once:

grep -q -x -F 'PATH=\"$PWD:$PATH\"' $HOME/.bash_profile || echo "PATH=\"$PWD:$PATH\"" >> $HOME/.bash_profile
MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • But i don't want append this line again and again if execute the script – TechChain Feb 04 '19 at 08:40
  • Even with the `sed ` command you show in your question you do not avoid that problem... See my updated answer to address that requirement. – MarcoS Feb 04 '19 at 08:42