开发者

bash: problem running command with quotes

开发者 https://www.devze.com 2023-02-20 08:23 出处:网络
In answering another question I create开发者_运维技巧d the following script bash script: #!/bin/bash

In answering another question I create开发者_运维技巧d the following script 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

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

Any ideas what I am doing wrong?

Thanks.


Use:

eval $cmd

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


BASH FAQ entry #50: "I'm trying to put a command in a variable, but the complex cases always fail!"

0

精彩评论

暂无评论...
验证码 换一张
取 消