Search code examples
bashaliasaws-clisingle-quotes

Configure alias with jq and multiple quotes


I'm trying to configure an alias that will give me a list of AWS resources and it keeps failing on escaping with single and double quotes.

aws ec2 describe-instances --filters "Name=tag:tag,Values=foo1" "Name=bar,Values=foo2,foo3" | jq '.Reservations[].Instances[].Tags[] | select(.Key=="Name") | .Value' | tr -d '"' |  sed -e 's/-pattern1.*//g' | sed -e 's/pattern2.*//g' | uniq

wrapping everything with single/double quotes does not work - alias alias1='aws ec2 describe-instances --filters "Name=tag:tag,Values=foo1" "Name=bar,Values=foo2,foo3" | jq '.Reservations[].Instances[].Tags[] | select(.Key=="Name") | .Value' | tr -d '"' | sed -e 's/-pattern1.*//g' | sed -e 's/pattern2.*//g' | uniq'

I'm trying to understand where shall I put the escaping characters


Solution

  • Using alias in the situation above will require escaping a lot of quotes

    My best advice is to use a function

    myFunc(){
    aws ec2 describe-instances --filters "Name=tag:tag,Values=foo1" "Name=bar,Values=foo2,foo3" | jq '.Reservations[].Instances[].Tags[] | select(.Key=="Name") | .Value' | tr -d '"' |  sed -e 's/-pattern1.*//g' | sed -e 's/pattern2.*//g' | uniq
     }
    

    You can put it in your .bashrc. Simply type myFunc to run the command

    No need to escape quote! So simple, happy coding