3

I'm trying to add an alias to my .bash_profile to do the following:

  • xx projname => cd ~/folder_1/projname and use node version 6 on nvm if nvm is currently using some other version
  • yy projname => cd ~/folder_2/projname and use node version 4 on nvm if nvm is currently using some other version

I have currently implemented everything except the last portion i.e. if nvm is currently using some other version like so:

function xx { cd ~/folder_1/"$1"; nvm use v6;}
function yy { cd ~/folder_2/"$1"; nvm use v4;}

What is the best way to handle the outstanding task? What I want is something like this:

run nvm current and see if index of v6 is false, and then run nvm use v6

But I'm really new to bash and can't seem to find a way to do this. TIA!

4
  • I am reading the question since I follow the bash tag, so no much knowledge of node.js. Could you indicate what is the way to check the nvm version you are running. Could it be nvm run node --version? Commented Aug 30, 2016 at 12:58
  • nvm current gives the current node version. The output is normally v6 nvm use <node-version> switches node versions Commented Aug 30, 2016 at 13:14
  • so the first word is always the nvm version? Also, does it show in stdout or stderr? You can check this saying nvm current 2>/dev/null. If it does show, it is stdout; otherwise, stderr. Commented Aug 30, 2016 at 13:16
  • Yeah. It does show in stdout Commented Aug 30, 2016 at 13:18

2 Answers 2

2

You want to get the version. From comments you say it comes like this:

$ nvm current
v6 <blabla>

So you need to catch the first word of nvm current's output:

read version _ <<< $(nvm current)

Then it is a matter of comparing the value with "v6". I would use:

if [ "$version" == "v6" ]; then
   ...
fi

All together:

function yy {
    cd ~/folder_2/"$1"
    read version _ <<< $(nvm current)
    if [ "$version" == "v6" ]; then
        nvm use v4
    fi
}
Sign up to request clarification or add additional context in comments.

1 Comment

That worked, thanks. I just needed to make the if condition a little wild cardy like so: if [[ "$version" == *"v6"* ]]; then
1

I am using this script for auto change my Node version, it works with Oh-my-zsh, make sure you have nvm installed then add below script to your .zshrc file

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

autoload -U add-zsh-hook
load-nvmrc() {
    local node_version="$(nvm version)"
    local nvmrc_path="$(nvm_find_nvmrc)"

    if [ -n "$nvmrc_path" ]; then
        local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")

        if [ "$nvmrc_node_version" = "N/A" ]; then
            nvm install
        elif [ "$nvmrc_node_version" != "$node_version" ]; then
            nvm use
        fi
    elif [ "$node_version" != "$(nvm version default)" ]; then
        echo "Reverting to nvm default version"
        nvm use default
    fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.