1

I have the following shell file that contains this:

sh
nightlyTag() {
    echo $1-alpha.$(date +%Y%m%d).$(($(date +%s%N)/1000000))
}

yarnPubCanaryVersion() {
    if [ -z "$1" ]
    then
        echo "No version argument supplied, maybe you meant v1.0.0?"
        return 1
    fi
    version=`nightlyTag $1`
    yarn version --new-version $version --no-git-tag-version
    npm publish --tag canary
    git reset --hard HEAD
}

I make the file executable with chmod +x canary.sh, then I run it doing ./canary.sh then my terminal changes to sh-3.2$ then I try to run the functions in the terminal like this nightlyTag and I get

sh: nightlyTag: command not found

Same for yarnPubCanaryVersion.

I was looking at this SO question

1
  • You use as first command sh. I don't know why you do it, but at this point the functions are not defined yet, because their definition comes after the sh command. So, at least, the sh would have to be the last command. But if you like sh as a shell so much, why don't you make it your login shell, and source the function definitions. Commented May 29, 2020 at 12:56

1 Answer 1

2

You won't be able to run functions from the terminal after you run the script.

You need to source the script to do this:

source ./canary.sh

Or add the contents of the file to the .bashrc file or its equivalent, and then source it.

The source command is used to load any function file into the current shell.

Now once you call those functions you will get the expected output.

At the top of your sh file you need to include:

#! /path/to/bash 

the path to the bash that you are using.

Sign up to request clarification or add additional context in comments.

4 Comments

That didn't work. I get the same results as doing ./canary.sh I feel it has to be related to this thorsten-hans.com/…
Yes, I am not sure about the Mac system but your terminal should still have a .bashrc file. Inside it you can directly post the contents of your .sh file, and then once you source it it will work.
@devpato at the top of your sh file you need to include: #! /usr/bin/bash Or the path to the bash that you are using.
@devpato I am happy that I could help.

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.