I'm trying to create a simple script in my package.json
file of my node project
{
"scripts": {
"db:create-migration": "echo 'Enter a filename' && read name && echo $name && npx sequelize-cli migration:generate --name $name --migrations-path db-migrations"
}
}
It's to call sequelize-cli
to create a db migration script with a provided name. It's called via the terminal like so:
npm run db:create-migration
When I echo $name
just after read
, it prints fine, but I get an Unbound variable "name"
with the sequelize-cli
call.
There's obviously something that eludes me, and most of the other questions are this are related to set -u
- which I'm guessing gets set somewhere behind the scenes when running this.
I'm a unix n00b, so apologies if this is a basic question. Environment is zsh on mac
npm
instead of the shell. If it tries to expand that$name
itself. (zsh giveszsh: name: parameter not set
, Bash givesbash: name: unbound variable
.) Try what happens if you run e.g.export VAR=foo
in the shell before runing that, and haveVAR=bar; echo $VAR
in the script...echo 'Enter a filename' && read name && export VAR=$name && echo $VAR && npx sequelize-cli migration:generate --name $VAR --migrations-path db-migrations
, but I still get the errorUnbound variable "name"
- this time before theecho
, so it's erroring on theexport
"scripts": { "db:create-migration": "VAR=bar; echo $VAR" }
in the file, and then runexport VAR=foo; npm run db:create-migration
from the shellbar