使用bash中引号的多层 [英] Using multiple layers of quotes in bash

查看:115
本文介绍了使用bash中引号的多层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个bash脚本,和我运行到一个引用的问题。

I'm trying to write a bash script, and I'm running into a quoting problem.

最终的结果,我以后是我的脚本调用:

The end result I'm after is for my script to call:

lwp-request -U -e -H "Range: bytes=20-30"

我的脚本文件是这样的:

My script file looks like:

CLIENT=lwp-request
REQ_HDRS=-U
RSP_HDRS=-e
RANGE="-H "Range: bytes=20-30""   # Obviously can't do nested quotes here
${CLIENT} ${REQ_HDRS} ${RSP_HDRS} ${RANGE}

我知道我不能使用嵌套引号。但我怎么能做到这一点?

I know I can't use nested-quotes. But how can I accomplish this?

推荐答案

通常情况下,你无法逃脱与内报价\\

Normally, you could escape the inner quotes with \:

RANGE="-H \"Range: bytes=20-30\""

但在运行命令&ndash的时候,这将不起作用;除非你把<$​​ C $ C>评估整个事情之前:

RANGE="-H \"Range: bytes=20-30\""
eval $CLIENT $REQ_HDRS $RSP_HDRS $RANGE

不过,因为你使用bash,不是的 SH 的,你可以把不同的论点数组:

However, since you're using bash, not sh, you can put separate arguments in arrays:

RANGE=(-H "Range: bytes=20-30")
$CLIENT $REQ_HDRS $RSP_HDRS "${RANGE[@]}"

这可以扩展到:

ARGS=(
    -U                             # Request headers
    -e                             # Response headers
    -H "Range: bytes=20-30"        # Range
)
$CLIENT "${ARGS[@]}"

这篇关于使用bash中引号的多层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆