Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing bash array as options into executable command

I have this simplified command, executed in a .bashrc function:

aws sso login --profile myprofile --cli-read-timeout 5

I’m trying to replace the command options with an array of options:

params=('--profile myprofile')
params+=('--cli-read-timeout 5')
aws sso login "${params[*]}"

AWS complains about unknown options:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help

Unknown options: --profile myprofile --cli-read-timeout 5

What is the correct format to be used in order to properly execute the command?

>Solution :

The option name and its argument must be separate array elements.

params=('--profile' 'myprofile')
params+=('--cli-read-timeout' '5')
aws sso login "${params[@]}"

or you can use --option=value syntax:

params=('--profile=myprofile')
params+=('--cli-read-timeout=5')
aws sso login "${params[@]}"

Also, you must use ${params[@]} rather than ${params[*]} so that each array element will be expanded to a separate word.

Your code is equivalent to combining all the parameters into a single argument:

aws sso login '--profile myprofile --cli-read-timeout 5'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading