4

I want to save output of an AWS CLI in a variable and use that variable in another AWS CLI, what I did is as follows:

taskarn= aws ecs list-tasks --cluster  mycluster --service-name "myService" --region "eu-west-1" --output text | grep "arn" | tr -d '"'

echo  $taskarn;  //empty
aws ecs stop-task --cluster mycluster --task $taskarn --region "eu-west-1"

when I echo $taskarn, it is empty.

Any help would be appreciated.

2 Answers 2

7

I used the following command and it works fine:

taskarn=$(aws ecs list-tasks --cluster  mycluster --service-name "myservice" --region "eu-west-1" | grep "arn" | tr -d '"')
echo  $taskarn;

aws ecs stop-task --cluster mycluster --task $taskarn --region "eu-west-1"
Sign up to request clarification or add additional context in comments.

2 Comments

When I run this, I get bash: [the-name-of-my-task-arn]: command not found error saved as the output to my variable
@Matrix How to access the values by keys?
1

Use backquote to execute the command and assign the result to the variable.

taskarn=`aws ecs list-tasks --cluster  mycluster --service-name "myService" --region "eu-west-1" --output text | grep "arn" | tr -d '"'`

But the correct way is to use the --query option of the CLI to extract what you want.

1 Comment

and what if the result is a dictionary? how to access the values by keys?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.