Anthony N.
Anthony N.

Reputation: 295

running bash file works , using source doesn't

So i am trying to create a function to run through the terminal.

I created the following bash file : myfile.sh

#!/bin/bash

fun(){
   echo "this is a function"
}
fun

Running the file with bash myfile.sh works perfectly.

If i try to use source myfile.sh so I can run the function from the terminal, it complains about the parenthesis : Badly placed ()'s.

Am I forgetting anything? I found many guides online and copied and pasted the code and still couldn't source the file.

Upvotes: 0

Views: 151

Answers (1)

merlin2011
merlin2011

Reputation: 75555

I was able to reproduce this problem when I executed the source myfile.sh after starting csh instead of bash. You can check your current shell using the command echo $SHELL.

If you want bash scripts to work when sourced, you will need to chsh your shell to /bin/bash.

Upvotes: 1

Related Questions