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

Patricio Vargas
  • 5,236
  • 11
  • 49
  • 100
  • 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. – user1934428 May 29 '20 at 12:56

1 Answers1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
bhristov
  • 3,137
  • 2
  • 10
  • 26
  • That didn't work. I get the same results as doing ./canary.sh I feel it has to be related to this https://thorsten-hans.com/5-types-of-zsh-aliases#functions-for-aliases-with-parameters – Patricio Vargas May 28 '20 at 17:48
  • 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. – bhristov May 28 '20 at 17:52
  • @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. – bhristov May 28 '20 at 17:54
  • 1
    @devpato I am happy that I could help. – bhristov May 28 '20 at 18:02