Search code examples
linuxbashunixcommon-expression-language

Bash Variable inside double quotes inside single quotes - Common Expression Language


I need to put variable inside a double quotes inside single quotes.

I want to create common expression language like below.

passbolt list resource --filter 'FolderParentID == "**some-imaginary-id**"'

Just help replace some-imaginary with some bash variable. Thanks in advance.


Solution

  • You need to concatenate separate strings via juxtaposition (i.e., putting them directly next to each other, with no intervening whitespace).

    passbolt list resource --filter 'FolderParentID == "'"$some_variable"'"'
    

    Note there are three separate quoted strings:

    1. FolderParentID == ", in single quotes
    2. $some_variable, in double quotes
    3. ", in single quotes

    Regardless of the type of quotes used to quote each of the three, the results are concatenated into a single word.

    At the expense of a command substitution, might want to use printf to generate the string for you.

    passbolt list resource --filter "$(printf 'FolderParentID == "%s"' "$some_variable")"
    

    Here you can "nest" the double quotes, because the command substitution starts a new quoting context: the first " inside the command substitution does not close the initial " that contains the command substitution.