JMESPath query expression with bash variable

TomH picture TomH · Oct 14, 2015 · Viewed 7.3k times · Source

Messing around with a simple aws cli query to check for the existence of a Lambda function and echo the associated role if it exists:

#!/bin/bash

fname=$1
role=$(aws lambda list-functions --query 'Functions[?FunctionName == `$fname`].Role' --output text)

echo "$fname role: $role"

However, $fname appears to be resolving to an empty string in the aws command. I've tried escaping the back ticks, swapping ` to ' and a miriad of other thrashing edits (and yes, I'm passing a string on the cl when invoking the script :)

How do I properly pass a variable into JMESPath query inside a bash script?

Answer

jamesls picture jamesls · Oct 14, 2015

Because the whole JMESPath expression is enclosed in single quotes, bash is not expanding the $fname variable. To fix this you can surround the value with double quotes and then use single quotes (raw string literals) for the $fname var:

aws lambda list-functions --query "Functions[?FunctionName == '$fname'].Role" --output text