Aniket Thakur
Aniket Thakur

Reputation: 68975

Bash script failing with unknown option due to space in argument

I am trying to run aws create lambda function. It goes as follows -

eval $(aws lambda create-function \
--function-name $FUNCTION_NAME \
--runtime $RUNTIME \
--role $ROLE \
--handler $HANDLER \
--region $REGION \
--zip-file $ZIP_FILE \
--profile $PROFILE \
--environment $env_variables)

All the variables come from command line. It is failing for env_variables. This gets constructed as -

env_variables="Variables={INPUT=${DAYS}}"

where DAYS is actually "20 days"

How can I avoid this space and pass my command successfully.

Upvotes: 2

Views: 2698

Answers (2)

Aniket Thakur
Aniket Thakur

Reputation: 68975

Finally following worked -

env_variables="\"Variables\":{\"INPUT\":\"${DAYS}\"}"

lambda_create_command="aws lambda create-function --function-name $FUNCTION_NAME --runtime $RUNTIME --role $ROLE --handler $HANDLER --region $REGION --zip-file $ZIP_FILE --profile $PROFILE --environment '$env_variables'"

echo "Executing command : $lambda_create_command"

eval $lambda_create_command

Important points -

  1. Quotes in env_variables
  2. Use of eval
  3. Single quote in command string i.e $env_variables

Reference - https://gist.github.com/andywirv/f312d561c9702522f6d4ede1fe2750bd

Complete working code :https://gist.github.com/aniket91/19492b32f570ece202718153661b1823

Upvotes: 1

Mark B
Mark B

Reputation: 200850

The value needs to be wrapped in quotes, try this:

env_variables="Variables=\"{INPUT=${DAYS}}\""

Upvotes: 2

Related Questions