-1

I have a code in a file named main.sh:

#!/bin/bash
function hello() {
  echo $1
}

I want to call it from command line like this:

main hello teddy

and it should output like hello teddy. Is that possible?. If yes, Please tell me. Thank you in advance

  • Does this answer your question? [Bash, call a function sourced from a script?](https://stackoverflow.com/questions/11330753/bash-call-a-function-sourced-from-a-script) – John Moon Mar 24 '21 at 15:32

1 Answers1

0

It is possible depending on where you define the function. There are several bash startup files which are run on startup. For example, .bashrc:

When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists.

So if you define your function in your .bashrc you will be able to run it just as if you had explicitly defined it using the shell.

joshmeranda
  • 3,001
  • 2
  • 10
  • 24