Leonardo Galski
Leonardo Galski

Reputation: 21

sed not expanding variable

I'm creating a script updater for a project, but I'm facing a weird issue in sed (using SLES 11 PL4). In the line below, bash/sed is simply not expanding the variable, no matter what I do:

oldvars=`sed -n '/#### Variables configurables ####/,/#### Variables configurables ####/p' ACCBackup.sh`; sed -i "/#### Variables configurables ####/,/#### Variables configurables ####/c\$oldvars" temp_ACCBackup.sh 

Also echoing the variable works fine:

oldvars=`sed -n '/#### Variables configurables ####/,/#### Variables configurables ####/p' ACCBackup.sh`; echo $oldvars                                   #### Variables configurables #### para='/mnt/BackupBBDD' # DIRECTORIO DESTINO compartido=TRUE # VAMOS A MONTAR ALGUN RECURSO COMPARTIDO? 'TRUE' O 'FALSE' compartido_externo='//IP/recurso' # RECURSO EN MAQUINA EXTERNA compartido_usuario='xxxx' # USUARIO PARA EL RECURSO COMPARTIDO compartido_contrasena='yyyy' # CONTRASENA PARA EL RECURSO COMPARTIDO #### Variables configurables ####

I tried changing the escape '\', putting the variable inside '{}'s, but none helped... This is getting weirder as I try new options. The system simply puts ONLY the variable name and not its content.

Can someone help me please?

Upvotes: 2

Views: 1547

Answers (2)

Varun Chandak
Varun Chandak

Reputation: 971

Or, you can use the below:

OLDVAR=abc
NEWVAR=xyz

sed -i 's/'"$OLDVAR"'/'"$NEWVAR"'/g' filename

Works for me everytime.

Upvotes: 2

Hasan Rumman
Hasan Rumman

Reputation: 597

Add another \ before the variable.

\\$oldvars

When you used single \ the $ is escaped and $oldvars became a normal string instead of variable.

Upvotes: 0

Related Questions