0

I am learning the bash function, and within the tutorial is the basic functions. My issue is that when I write the function on the terminal it works, but when I write it inside a .sh file and try to execute the function by its name it doesn't work

This is the terminal trails:

Input:

$ hello_world () { echo ' hello world this is 2021' ;}

run:

$ hello_world

hello world this is 2021

This is the hello_world.sh

$ vi hello_world2.sh

#!/bin/bash

hello_world2 () {
        echo ' hello world this is 2022' ;
}

when I try to execute it I get this error:

$ hello_world2

zsh: command not found: hello_world2
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
Alhu.A
  • 31
  • 1
  • You need to source the file first, to define the function. `. ./hello_world2.sh`. – chepner Nov 22 '21 at 14:31
  • 1
    You aren't using `bash` at all, though: you are using `zsh`. – chepner Nov 22 '21 at 14:32
  • I tried `. ./hello_world2.sh `and it did not work. – Alhu.A Nov 22 '21 at 14:34
  • Where is `hello_world2.sh` located? You'll need the correct path; I assumed it was in the current working directory. – chepner Nov 22 '21 at 14:36
  • I also tried: ` bash hello_world2.sh` and ` source hello_world2` and did not work too – Alhu.A Nov 22 '21 at 14:37
  • As your interactive shell is `zsh`, not `bash`, why do you try to define and call a `bash` function? Wouldn't it be more natural to define a `zsh` function or to run all these commands in an interactive `bash` session? – Renaud Pacalet Nov 22 '21 at 14:37
  • yes, it is in the current directory – Alhu.A Nov 22 '21 at 14:37
  • @RenaudPacalet He did define a `zsh` function; the POSIX-compliant syntax shown here will work in `bash` or `zsh`. – chepner Nov 22 '21 at 14:38
  • I'm new to these functions, dose zsh functions have the same syntax as bash functions? and what is the best practice to run in an interactive bash session or move to zsh functions ? and Thank you – Alhu.A Nov 22 '21 at 14:40
  • @chepner You're right but I wonder if there would not be a kind of big confusion in the OP's mind between these two shells and between sourcing and executing a script... – Renaud Pacalet Nov 22 '21 at 14:40
  • @chepner why it's not working with me ? – Alhu.A Nov 22 '21 at 14:41
  • 1
    Well, your function is valid both in `bash` and `zsh`. So you can source your file (`source hello_world2.sh` or `. hello_world2.sh`) and then call your function (`hello_world2`). You need to source only once for a given zsh or bash session. Note that if this script is really intended for sourcing, the shebang line is not needed. – Renaud Pacalet Nov 22 '21 at 14:42
  • If you source the file that will **define** the function. It doesn't **run** the function automatically after defining it, but it does make it available to run without needing any further steps. – Charles Duffy Nov 22 '21 at 14:43
  • I have no idea; what exactly happens when you do `. ./hello_word2.sh`, then try to run `hello_world2`? – chepner Nov 22 '21 at 14:44
  • @RenaudPacalet It worked, Thank you so much .. is there any resources you recommend that explain this more? – Alhu.A Nov 22 '21 at 14:44
  • What needs explaining? `source`ing a file runs everything in that file as if you entered it into your shell directly. Just entering the definition of a function doesn't actually run that function when you do it interactively, so it also doesn't do it when you're sourcing a file. – Charles Duffy Nov 22 '21 at 14:45
  • @Alhu.A I always recommend reading the bash manual `man bash`. It is very complete and well written. But there are certainly more user-friendly resources and tutorials on the web. – Renaud Pacalet Nov 22 '21 at 14:46
  • @chepner this is also worked when i tried the `. ./hello_word2.sh` and then called the function name, Thank you .. is this `. ./hello_word2.sh` an alternative to `sourec` ? – Alhu.A Nov 22 '21 at 14:46
  • `source` is a non-standard (though far more readable) synonym for `.`. – chepner Nov 22 '21 at 14:47
  • Thank you all for the help – Alhu.A Nov 22 '21 at 14:47
  • Finally -although it does not matter in your toy example - I am still puzzled why you try to execute functions written in bash by zsh..... – user1934428 Nov 22 '21 at 15:08
  • `print 'hello world this is' $(date +%Y)` – Elliott Frisch Nov 22 '21 at 15:08
  • @Alhu.A : chepner is partially right with his explanations. Your question is really difficult to understand, because you have bash and zsh in your tags, and we don't know which shell you are talking about. In bash, `source` and `.` are indeed the same, as chepner said. In zsh, they are not. Please read the man page, and, please, next time ask more precisely what you want to know. – user1934428 Nov 24 '21 at 12:52

1 Answers1

-1

If you are trying to run the script, there are two problems:

  1. You are trying to execute a script which is not on the PATH

  2. Your script is not executable

The solution:

# make the script executable
chmod u+x path/to/hello_world2.sh

# run the script
path/to/hello_world2.sh

If you are attempting to call the function inside the script...

The reason why your first sample worked the way it did was because the function was already defined inside the shell, so it was simply a matter of searching the $functions array to find the function, and the runs the commands inside it.

A function name, is very different from the name of the file containing the function.

If your goal was to run the function inside the script, then you can forgo the above steps, and do this instead:

# import the function into the current shell
source path/to/hello_world2.sh

# run the function
hello_world2
smac89
  • 39,374
  • 15
  • 132
  • 179