0

I just came across this command:

npm run script ./src/automation/automation_main.ts -- -i payroll_integration

I googled about the double dash and it appears to signify the end of command options, per this answer: https://unix.stackexchange.com/a/11382/47958

What I don't understand is why there are command options after the double dash (the -i). Can we still include command options even after the double dash?

I ran the above script with and without the double dash, and both appear to run.

ilkkachu
  • 138,973
Bob Horn
  • 103
  • It's not about Bash, but the command you run. It's the one that interprets the arguments it gets, including the double-dash. – ilkkachu Jul 18 '22 at 17:52

1 Answers1

3

The situation in your example command is there are two programs being invoked, and both of them use command-line arguments. You are invoking npm, and npm will obey the run script arguments to invoke the script automation_main.ts. None of the arguments are enclosed in quotes (perhaps that's necessary for this kind of npm command).

The argument -i payroll_integration is clearly intended for the script and not for npm. How do you convince npm to not try to parse it (which will probably make it error out)?

The answer: you insert an argument that tells npm that the rest of words on the line are not npm's arguments. This is --, which means "your arguments stop here, don't worry about the rest". Npm will remove its arguments up to and including the --, and invoke the script with the rest of the line present for the script to parse and use.

Note that, while bash and npm understand the -- argument (as many, many other GNU utilities do), there are programs that don't understand it and won't behave the way I described here for npm.

Sotto Voce
  • 4,131