[SOLVED] bash -c nvm not working

Issue

This Content is from ask ubuntu. Question asked by Alex

I have installed nvm and it was appended to my /home/www-data/.bashrc

Calling nvm works.

bash
nvm

works

but

$ bash -c nvm
bash: nvm: command not found

does not work.

same when I specific the rcfile

$ bash  --rcfile /home/www-data/.bashrc -c "nvm"
bash: nvm: command not found

Why is that?

In the end-result I want to use nvm in RoboFile exec() command and tried to wrap it into bash -c because nvm is a bash function and not a real script.

Solution

When invoked with -c, bash runs as a non-interactive shell. The default interactive shell startup files (/etc/bash.bashrc~/.bashrc) are not read. Even if you force ~/.bashrc to be read, its body is likely not executed – the default Ubuntu one (copied from /etc/skel during account creation) for example includes the lines

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

which bypasses everything following.

You could force an interactive shell using bash -ic, however that may cause other issues depending on what else is in your .bashrc.

Answered by steeldriver

This Question and Answer are collected from askubuntu, is licensed under the terms of CC BY-SA 2.5. CC BY-SA 3.0. CC BY-SA 4.0.

people found this article helpful. What about you?