0

I have some code like

#!/bin/bash
DIR='/project/stnb'
HOST_CONFIG="${DIR}/config/host.$(hostname -s).php"
CT_NUMBER="$(grep -Pow --max-count=1  ct'[0-9]*'  ${HOST_CONFIG})"  #number of container 
BASH_PROFILE_PS_STING="PS1='\[\e[0;32m\]\u\[\e[0;32m\]@\[\e[0;36m\]${CT_NUMBER}\[\e[0;33m\]\w\[\e[0;36m\]\$(__git_ps1 \"(%s)\") \[\e[0m\]\$ '"

so I want to rewrite 1st row at "bash_profile", and try "sed"

sed -i "1с${BASH_PROFILE_PS_STING}" ~/.bash_profile

but after replacing I see only

PS1='[e[0;32m][e[0;32m]@[e[0;36m]ct88[e[0;33m]w[e[0;36m]$(__git_ps1 "(%s)") [e[0m]$ '

After googling I was try following options:

1) Use an alternate regex delimiter

sed -i "1s!.*!${BASH_PROFILE_PS_STING}!" ~/.bash_profile

2) or

sed -i "1c~${BASH_PROFILE_PS_STING}" ~/.bash_profile

what's wrong? Is it really necessary to preliminary replace \ in variable before and replace change after operation back?

  • 2
    I'm having trouble working out what output you want. Could you edit your question to add example output given that input, please? – Jon Oct 01 '19 at 12:11
  • Try doubling (or tripling) the "\" chars. Good luck. – shellter Oct 01 '19 at 12:28
  • The backslash has a special meaning to `sed`; to produce a literal backslash into the script, you have to escape it first from the shell, and then also from `sed`. – tripleee Oct 01 '19 at 12:35

1 Answers1

-3

You need to specify the replacement string, like:

sed -i 's!OLD_STRING!NEW_STRING!' ~/.bash_profile
Stphane
  • 3,368
  • 5
  • 32
  • 47