-1

I try to replace variable in a file but the variable is not replaced after the execution of the script. i just see that my file was modified with the date when i run the script.

Here my script:

    #!/bin/bash
###################################################################
#
# NOM   : CONFIG_PROFIL
#     
###################################################################

##--------variable---------->>
DEST_DIR="profil"
chemin_env="/ords/test/profilprd"



sed -i 's/^$env/${chemin_env}/' $DEST_DIR/index_prive.htm


echo -*-*- Traitement terminé  -*-*

Here my file with the variable that i want to replace :

<frame name="mainFrame" src="$env/PG_LOGIN.pLogin?msg=" marginwidth="5" >

What i'm doing wrong ?

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
Mathieu Mourareau
  • 1,069
  • 1
  • 13
  • 32
  • you should put escape char before dollar sign (which is in front of env ) Modify as below : sed -i 's/\$env/${chemin_env}/' $DEST_DIR/index_prive.htm – Ravi Kulkarni Sep 16 '19 at 09:22
  • many thanks it's working but now in my file i get ${chemin_env} and not the content of the variable @RaviKulkarni – Mathieu Mourareau Sep 16 '19 at 09:28
  • `sed -i "s,^$env,${chemin_env}," $DEST_DIR/index_prive.htm` – Wiktor Stribiżew Sep 16 '19 at 09:36
  • ok. you just need double quotes instead of single quotes in sed statement so script understand its varaible and needs to replaced with its vale : sed -i "s/\$env/${chemin_env}/" $DEST_DIR/index_prive.htm – Ravi Kulkarni Sep 16 '19 at 09:53

1 Answers1

1

Till what I understand your question, you should use sed like:

sed -i "s~\$env~${chemin_env}~" $DEST_DIR/index_prive.htm

In your question:

's/^$env/${chemin_env}/'

1) You have used single quotes which prevent any substitution. So value of ${chemin_env} won't get substituted.

2) Also ^ means beginning of the string and in your file $env is not in beginning of the string.

3) To prevent value of $env from getting substituted, you need to use it like \$env.

4) After making the above 3 corrections if the value of ${chemin_env} get substituted, it won't work because the variable contains / which you are using as delimeter in your sed expression. That's why I used ~ as delimeter instead. What delimiters can you use in sed?

Mihir
  • 4,125
  • 2
  • 10
  • 31