Linux Bash。两行之间有什么区别?

时间:2022-01-15 13:28:23

I have a following bash script:

我有以下bash脚本:

 1  #!/bin/bash
 2  query='query= SELECT * WHERE { ?s ?p ?o } LIMIT 5'
 3  cmd="curl $1 -s -d \"$query\""
 4  echo "$cmd"
 5  # curl $1 -s -d "$query"
 6  # $cmd

5th and 6th lines must do the same. When i uncomment the 5th line, everything works fine. But with the 6th line nothing doesn't work.

第五和第六行也必须这样做。当我取消第5行注释时,一切正常。但是在第六行,什么都不行。

So i'm wondering whats the difference?

我想知道这有什么区别吗?

Thanks.

谢谢。

2 个解决方案

#1


1  

Line 5 passes $query as a single argument. Line 6 passes each word of $query as a separate argument, with " at the beginning of the first and " at the end of the last. Put your arguments in an array instead.

第5行将$query作为单个参数传递。第6行将$query的每个单词作为单独的参数传递,其中“在第一个的开头”和“在最后一个的末尾”。将参数放在数组中。

#2


2  

No; in line 4 you are just displaying the command by "echo"ing it; line 6 actually executes the command.

没有;在第4行中,您只是通过“echo”显示命令;第6行实际执行命令。

E.g.

如。

$eg="ls /var/www"
echo $eg #This would literally return ls /var/www"
$eg #This would return the directory listing of /var/www/ (actually run the command).

#1


1  

Line 5 passes $query as a single argument. Line 6 passes each word of $query as a separate argument, with " at the beginning of the first and " at the end of the last. Put your arguments in an array instead.

第5行将$query作为单个参数传递。第6行将$query的每个单词作为单独的参数传递,其中“在第一个的开头”和“在最后一个的末尾”。将参数放在数组中。

#2


2  

No; in line 4 you are just displaying the command by "echo"ing it; line 6 actually executes the command.

没有;在第4行中,您只是通过“echo”显示命令;第6行实际执行命令。

E.g.

如。

$eg="ls /var/www"
echo $eg #This would literally return ls /var/www"
$eg #This would return the directory listing of /var/www/ (actually run the command).