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.

helloV
  • 50,176
  • 7
  • 137
  • 145
Matrix
  • 2,399
  • 5
  • 28
  • 53

2 Answers2

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"
Matrix
  • 2,399
  • 5
  • 28
  • 53
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.

helloV
  • 50,176
  • 7
  • 137
  • 145