Skip to content

nvm bash compatibility issue: Unbound variable: PATTERN #3820

@arizvisa

Description

@arizvisa

nvm debug output:

Details
$ nvm debug
nvm --version: v0.40.4
$SHELL: /bin/bash
$SHLVL: 1
whoami: 'user'
${HOME}: /home/user
${NVM_DIR}: '${HOME}/.nvm'
${PATH}: ${NVM_DIR}/versions/node/v14.17.3/bin:${HOME}/.local/bin:${HOME}/.npm/bin:${NVM_DIR}/versions/node/v14.17.3/bin:${HOME}/.config/yarn/global/node_modules/.bin:${HOME}/.yarn/bin:${HOME}/bin:${HOME}/.local/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin
-bash: PREFIX: unbound variable
$PREFIX: ''
-bash: NPM_CONFIG_PREFIX: unbound variable
${NPM_CONFIG_PREFIX}: ''
-bash: NVM_NODEJS_ORG_MIRROR: unbound variable

nvm ls output:

Details
$ nvm ls
->     v14.17.3
       v16.20.2
       v20.18.0
       v20.19.5
       v22.20.0
       v24.14.1
default -> v14.17.3 *
iojs -> N/A (default)
node -> stable (-> v24.14.1 *) (default)
stable -> 24.14 (-> v24.14.1 *) (default)
unstable -> N/A (default)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 *
lts/hydrogen -> v18.20.8 (-> N/A)
lts/iron -> v20.20.2 (-> N/A)
lts/jod -> v22.22.2 (-> N/A)
lts/krypton -> v24.14.1 *
lts/* -> lts/krypton (-> v24.14.1 *)

How did you install nvm?

install script

What steps did you perform?

$ nvm ls-remote

What happened?

Standard unbound variable issue.

What did you expect to happen?

Non-failure.

Is there anything in any of your profile files that modifies the PATH?

Yes, but PATH is irrelevant with regards to this issue.

Comments

There doesn't appear to be a section in this template for describing how to fix the issue, but anyways...

The lines of code that follow are from the tag v0.40.4 in the repository.

This loop inside the case for "ls-remote" will not be executed if there are no parameters specified.

nvm/nvm.sh

Lines 4187 to 4220 in 62387b8

while [ $# -gt 0 ]; do
case "${1-}" in
--) ;;
--lts)
NVM_LTS='*'
;;
--lts=*)
NVM_LTS="${1##--lts=}"
;;
--no-colors) NVM_NO_COLORS="${1}" ;;
--*)
nvm_err "Unsupported option \"${1}\"."
return 55
;;
*)
if [ -z "${PATTERN-}" ]; then
PATTERN="${1-}"
if [ -z "${NVM_LTS-}" ]; then
case "${PATTERN}" in
'lts/*')
NVM_LTS='*'
PATTERN=''
;;
lts/*)
NVM_LTS="${PATTERN##lts/}"
PATTERN=''
;;
esac
fi
fi
;;
esac
shift
done

Then at line 4224, the "PATTERN" variable is used without having a default value. Easy fix is to replace "${PATTERN}" with "${PATTERN:-}" similar to how it's used in multiple places throughout the library.

nvm/nvm.sh

Lines 4222 to 4229 in 62387b8

local NVM_OUTPUT
local EXIT_CODE
NVM_OUTPUT="$(NVM_LTS="${NVM_LTS-}" nvm_remote_versions "${PATTERN}" &&:)"
EXIT_CODE=$?
if [ -n "${NVM_OUTPUT}" ]; then
NVM_NO_COLORS="${NVM_NO_COLORS-}" nvm_print_versions "${NVM_OUTPUT}"
return $EXIT_CODE
fi

While running nvm debug for this issue, there were some more undefined variables. Since this is for debugging, maybe it's preferred to output an error message on a different line in order to distinguish whether an environment variable is undefined without using a placeholder value such as "<MISSING>" or something... but, that's up to the maintainer.

The lines of code in the following snippet contain the code relevant to nvm debug. Lines 3258 contains the reference to "PREFIX", with the following line 3259 referencing "NPM_CONFIG_PREFIX". Then at line 3260 is the reference to "NVM_NODEJS_ORG_MIRROR".

nvm/nvm.sh

Lines 3253 to 3264 in 62387b8

nvm_err "\$SHLVL: ${SHLVL-}"
nvm_err "whoami: '$(whoami)'"
nvm_err "\${HOME}: ${HOME}"
nvm_err "\${NVM_DIR}: '$(nvm_sanitize_path "${NVM_DIR}")'"
nvm_err "\${PATH}: $(nvm_sanitize_path "${PATH}")"
nvm_err "\$PREFIX: '$(nvm_sanitize_path "${PREFIX}")'"
nvm_err "\${NPM_CONFIG_PREFIX}: '$(nvm_sanitize_path "${NPM_CONFIG_PREFIX}")'"
nvm_err "\$NVM_NODEJS_ORG_MIRROR: '${NVM_NODEJS_ORG_MIRROR}'"
nvm_err "\$NVM_IOJS_ORG_MIRROR: '${NVM_IOJS_ORG_MIRROR}'"
nvm_err "shell version: '$(${SHELL} --version | command head -n 1)'"
nvm_err "uname -a: '$(command uname -a | command awk '{$2=""; print}' | command xargs)'"
nvm_err "checksum binary: '$(nvm_get_checksum_binary 2>/dev/null)'"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions