peonicles
peonicles

Reputation: 1637

Passing a variable inside quotation marks to a command in bash

Some context :

I'm attempting to run a command as part of a bash script to poll for system/device data.

# Recommended usage in the command line, contains single and double quotes
wbemcli ein 'http://localhost:5988/root/cimv2:some_class'
wbemcli gi 'http://localhost:5988/root/cimv2:some_class.DeviceID="Some Device ID"'

# I can get away with just 1 level of quotation marks
wbemcli ein http://localhost:5988/root/cimv2:some_class
wbemcli gi http://localhost:5988/root/cimv2:some_class.DeviceID="Some Device ID"
wbemcli gi http://localhost:5988/root/cimv2:some_class.DeviceID='Some Device ID'

So this is working ...

    #!/bin/sh

    C_PATH=http://localhost:5988/root/cimv2
    CLASS=some_class
    ID="Some Device ID"

    set -x
    wbemcli ein $C_PATH:$CLASS

Unfortunately, it falls apart when I attempt to integrate the quotation marks into the command. The code executed in the shell is unexpected to me.

    # Code
    wbemcli gi $C_PATH:$CLASS.DeviceID=\"$ID\"

    output $ ./myscript
    + wbemcli gi 'http://localhost:5988/root/cimv2:some_class.DeviceID="Some' Device 'ID"'

    # I was expecting this ...
    output $ ./myscript
    + wbemcli gi http://localhost:5988/root/cimv2:some_class.DeviceID="Some Device ID"




Bash is adding quotation marks in places I didn't expect. It's even enclosing the entire URL portion in single quotation marks. What's going on ?

Upvotes: 1

Views: 2646

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200503

Try like this:

wbemcli gi -nl "$C_PATH:$CLASS.DeviceID=\"$ID\""

or like this:

wbemcli gi -nl "$C_PATH:$CLASS.DeviceID='$ID'"

Upvotes: 1

Related Questions