Skip to content
Advertisement

Shell: how to create a list variable from several args

I have shell script that runs a cli command with 3 or more args, i can’t specify in advance the number of args that will be passed to the script.

cmd=$(aws emr add-steps --cluster-id ${1} --steps Type=CUSTOM_JAR, Jar=s3://elasticmapreduce/libs/script-runner/script-runner.jar, Name="$2",Args=[$3,$4,....])

i tried to replace Args=[$3,$4,....] by Args=[${@:3}] but the arguments are not comma-separated.

Advertisement

Answer

Change the value of IFS inside the command substitution and use $* instead of $@:

cmd=$(IFS=,;
      aws emr add-steps --cluster-id ${$1} 
         --steps Type=CUSTOM_JAR, 
         Jar=s3://elasticmapreduce/libs/script-runner/script-runner.jar, 
         Name="$2",Args=["${*:3}"])

The semicolon after IFS=, is optional as long as the call to aws begins on the next line, not immediately after the assignment.

${$1} is an syntax error, by the way.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement