1

I'm trying to pass the description from my package.json file up to AWS. The package description is a string like so:

"description": "A simple hello world from my web app",

And the npm script calls the aws command line and needs a description for the Lambda function:

"scripts": {
  "create": "aws lambda create-function --function-name $npm_package_name --description $npm_package_description
}

But $npm_package_description ends up becoming multiple parameters in the aws call. How do I pass the result of $npm_package_description as a string?

This is the error I get, by the way:

Unknown options: A, simple, hello, world, from, my, web, app

msanford
  • 11,803
  • 11
  • 66
  • 93
Costa Michailidis
  • 7,691
  • 15
  • 72
  • 124

1 Answers1

1

Tried something random and it worked.

Wrapping '$npm_package_description' in single quotes didn't help, it just stayed a string, but apparently escaped double quotes worked.

Like so:

"scripts": {
  "create": "aws lambda create-function --function-name $npm_package_name --description \"$npm_package_description\"
}

Can anyone explain why this works?

msanford
  • 11,803
  • 11
  • 66
  • 93
Costa Michailidis
  • 7,691
  • 15
  • 72
  • 124
  • 1
    It's because of _Word spitting_ as per @msanford comment. By placing the `$npm_package_description` variable in double quotes you prevent word splitting, and because it's JSON that's why you have to escape the double quotes. i.e. `\"...\"`. – RobC Oct 29 '18 at 18:03
  • 1
    @Costa Because [single quotes do not trigger _parameter expansion_](https://stackoverflow.com/a/13802438/114900), meaning anything within them is treated literally. Try a little test in a bash console: `read a` and enter `Hello World` (and hit enter). Then execute `$a`, and `'$a'` and `"$a"` individually and observe the errors. `'$a'` will complain that the literal string `$a` is not found, whereas `"$a"` will complain that `"Hello"` is not found (the expanded parameter). – msanford Oct 29 '18 at 19:59
  • 2
    Single quotes remove the special meaning of all characters between the quotes, in your case that means the reference to `$npm_package_description` (i.e. the dollar `$`) is ignored. This results in all chars inside single quotes becoming a literal string. However, when using Double quotes every substitution beginning with a dollar sign `$` is performed. More info [here](http://mywiki.wooledge.org/Quotes#Types_of_quoting). – RobC Oct 29 '18 at 20:05
  • 1
    I'll offer a generalization: bash-like scripting is intricate and unforgiving, in favour of being extremely terse. So, while many languages treat `"` and `'` as the same thing (or, [for that matter, `a=1` and `a = 1`](https://stackoverflow.com/a/4977390/114900)), bash does not. This can lead to much unexpected behaviour when coming from another language. – msanford Oct 29 '18 at 20:13
  • 1
    @Costa Check out Greg's blog above, excellent resource. Then, put scripts through [shellcheck.net](https://shellcheck.net) (past contributor) and read through tags on [unix.se]. Have fun! Once you get the hang of it, it's actually extremely powerful, and a great tool in the belt. – msanford Oct 29 '18 at 20:51
  • 1
    Also, accept your own answer: it was the correct one, after all! You can flag this comment as "No Longer Necessary" once you have, if you like. – msanford Oct 29 '18 at 20:53