0

I've been trying to wrap my head around this for over an hour now and my searches haven't helped yield the answer.

Trying to set a variable inside a bash script. This variable is taking variable-A and removing variable-B from it.

Prefix="$(echo ${Process} | sed -e 's/${Server}//g')"

So if Process=abcd1wxyz01 and Server=wxyz01, then Prefix should end up being abcd1.

I've tried so many iterations from online searches I honestly can't recall what all I've tried.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116

2 Answers2

2

Your problem are the quotes, as pointed out in afsal_p's answer.

You could do this with parameter expansion instead:

$ process=abcd1wxyz01
$ server=wxyz01
$ prefix=${process%"$server"}
$ echo "$prefix"
abcd1

The ${word%suffix} expansion removes suffix from the end of word.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
0

please use " instead of ' while using bash variables inside sed:

Prefix="$(echo ${Process} | sed -e "s/${Server}//g")"
echo $Prefix
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
afsal_p
  • 139
  • 3
  • `echo "${Process}"` or `echo "$Process"` would also be more correct than `echo ${Process}`; see [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo). Similarly, `echo $Prefix` should be `echo "$Prefix"` – Charles Duffy Aug 23 '18 at 18:00
  • Thank you so much for the clarification – afsal_p Aug 23 '18 at 19:48