npm and yarn v1
The shell on unix like systems is sh
so stick to POSIX sh definitions.
A default for a null or undefined value in sh
is ${VAR:-default}
cat: "echo \"cat ${1:-Rufus}\""
yarn 2+ berry
Use a minimal sh
implementation yarnpkg-shell
which supports the basic shell syntax but is not fully POSIX compliant. This enables all environments, with or without sh
, to execute package.json
scripts in the same manner.
Test running the following:
printf 'VAR= %s\n' "$VAR"
printf 'VAR- %s\n' "${VAR-def}"
printf 'VAR:- %s\n' "${VAR:-def}"
printf 'VAR+ %s\n' "${VAR+def}"
printf 'VAR:+ %s\n' "${VAR:+def}"
via:
{
"name": "so36729207-npm-sh",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"sh": "printf 'VAR= %s\n' \"$VAR\"; printf 'VAR- %s\n' \"${VAR-def}\"; printf 'VAR:- %s\n' \"${VAR:-def}\"; printf 'VAR+ %s\n' \"${VAR+def}\"; printf 'VAR:+ %s\n' \"${VAR:+def}\""
}
}
Produces the same results for sh
in dash/Debian, ash/Alpine and zsh/macos environment
$ docker run --rm so36729207/alpine npm run sh
> so36729207-npm-sh@1.0.0 sh
> printf 'VAR= %s
> ' "$VAR"; printf 'VAR- %s
> ' "${VAR-def}"; printf 'VAR:- %s
> ' "${VAR:-def}"; printf 'VAR+ %s
> ' "${VAR+def}"; printf 'VAR:+ %s
> ' "${VAR:+def}"
VAR=
VAR- def
VAR:- def
VAR+
VAR:+
$ docker run --rm --env VAR= so36729207/alpine npm run sh
> so36729207-npm-sh@1.0.0 sh
> printf 'VAR= %s
> ' "$VAR"; printf 'VAR- %s
> ' "${VAR-def}"; printf 'VAR:- %s
> ' "${VAR:-def}"; printf 'VAR+ %s
> ' "${VAR+def}"; printf 'VAR:+ %s
> ' "${VAR:+def}"
VAR=
VAR-
VAR:- def
VAR+ def
VAR:+
$ docker run --rm --env VAR=a so36729207/alpine npm run sh
> so36729207-npm-sh@1.0.0 sh
> printf 'VAR= %s
> ' "$VAR"; printf 'VAR- %s
> ' "${VAR-def}"; printf 'VAR:- %s
> ' "${VAR:-def}"; printf 'VAR+ %s
> ' "${VAR+def}"; printf 'VAR:+ %s
> ' "${VAR:+def}"
VAR= a
VAR- a
VAR:- a
VAR+ def
VAR:+ def