Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何在单引号中使用主机名变量?_Bash_Quoting - Fatal编程技术网

Bash 如何在单引号中使用主机名变量?

Bash 如何在单引号中使用主机名变量?,bash,quoting,Bash,Quoting,在名为input_data的bash文件中 LA=$1 curl -H 'Content-Type: application/x-ndjson' -XPOST '"'$LA'":9200/human_flow/human_flow/_bulk?pretty' --data-binary @$2.json 运行命令时 ./input_data localhost human_flow 给出错误消息 bash-3.2$ ./input_data localhost human_flow cur

在名为input_data的bash文件中

LA=$1
curl -H 'Content-Type: application/x-ndjson' -XPOST '"'$LA'":9200/human_flow/human_flow/_bulk?pretty' --data-binary @$2.json
运行命令时

 ./input_data localhost human_flow
给出错误消息

bash-3.2$ ./input_data localhost human_flow
curl: (6) Could not resolve host: "localhost"
可以解析localhost

bash-3.2$ ping localhost
PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.067 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.156 ms
使用127.0.0.1而不是localhost

bash-3.2$ ./input_data 127.0.0.1 human_flow
curl: (6) Could not resolve host: "127.0.0.1"

正确的用法如下所示:

curl \
  -H 'Content-Type: application/x-ndjson' \
  -XPOST \
  --data-binary "@$2.json" \
  "$LA:9200/human_flow/human_flow/_bulk?pretty"
所有展开式都用双引号括起来,既不是用单引号括起来的,也不是用引号括起来的。 如果期望双引号具有语义含义,则双引号不得位于单引号内。 根据POSIX约定,应该在所有可选参数之后指定位置参数。遵循GNU约定的应用程序并不严格要求这样做,但是全面遵循它可以避免意外。
我的意思是很明显它不能解析localhost。为什么在它的周围嵌入了双引号?curl-H'内容类型:application/x-ndjson'-XPOST'$LA:9200/human_flow/human_flow/_bulk?pretty'-data binary@$2.json也有错误,没错。为什么你认为你需要使用单引号,或者使你的双引号文字或其他?解决了。谢谢。说得清楚一点,你应该使用双引号——如果你完全不使用引号的话,它看起来会工作,但会有问题。看见