Bash: issue with running command with quotes

Answering another question, I created the following bash script:

#!/bin/bash files1=( file1.txt file2.txt file3.txt ) files2=( file1_.txt file2_.txt file3_.txt ) cmd="vim -c 'set diffopt=filler,vertical' -c 'edit ${files1[0]}' -c 'diffsplit ${files2[0]}' " echo $cmd for i in {1..2}; do cmd="${cmd} -c 'tabe ${files1[i]}' -c 'diffsplit ${files2[i]}' " done #$cmd echo $cmd 

the problem is that if I try to run

 $cmd 

I get errors at the end of the script, but if I just use echo $ cmd and then copy and paste on the command line, it works fine.

Any ideas what I'm doing wrong?

Thanks.

+4
source share
2 answers

Using:

 eval $cmd 

So that the variables inside the expression are expanded before execution.

+8
source

Source: https://habr.com/ru/post/1345253/


All Articles