Using multiple layers of quotes in bash

I am trying to write a bash script and I am facing a quotation mark problem.

The end result that I get for my script calls:

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

My script file is as follows:

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 that I cannot use nested quotes. But how can I do this?

+5
source share
3 answers

You can usually avoid internal quotes with \:

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

But this will not work when you run the command - unless you put evalbefore everything:

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

However, since you are using bash, not sh, you can put individual arguments into arrays:

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

This can be continued:

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

, "", "" .
:

x='Say "hi"'
y="What up?"
+1

try the following:

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

you can use '' and \

NO_ERROR = 'errors = \ "0 \"' '';

0
source

All Articles