0

I am currently using bash scripts to edit files using vim, one such example of a line in my scripts is:

vim Inference.ini -c '%s/Train/Inf/g | wq'

which opens Inference.ini, replaces all instances of 'Train' with 'Inf' and saves and quits the file.

What I want to do is to pass a script variable into the vim command, which I thought would look something like:

${my_var}='Hello World'
vim Inference.ini -c '%s/Train/${my_var}/g | wq'

But unfortunately the above command does not work, the file opens but nothing is changed.

Inian
  • 80,270
  • 14
  • 142
  • 161
PedsB
  • 311
  • 2
  • 9
  • The variables in `bash` don't take a `$` prefix before them. Also to pass the variable under a single quoted command, add an extra layer of double-quotes with single quote on top, i.e. `my_var='Hello World' ; vim Inference.ini -c '%s/Train/'"${my_var}"'/g | wq'` – Inian May 13 '19 at 10:01
  • You're absolutely right, silly mistake on my part. I tried your solution but I'm met with a "trailing characters" error. My problem is a bit more complicated in that the replacement and pattern strings have spaces in them as so: %s/--model_dir $working_directory/--model_dir '"${my_var}"'/g – PedsB May 13 '19 at 10:49

0 Answers0