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
  • 46,329
  • 4
  • 117
  • 135
Matrix
  • 1,915
  • 4
  • 23
  • 45

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
  • 1,915
  • 4
  • 23
  • 45
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
  • 46,329
  • 4
  • 117
  • 135