The trick to making a bash script callable from the command line with specific function names is the "$@" at the end. Without it, defining functions in a script does nothing useful — you’d have to source the script and call them manually. This pattern makes scripts behave like a proper CLI with named subcommands.
- Create an empty file with .sh extension
- Use your favourite editor add a method to the file
1
2
3
4
5
6
7
8
9
| $ vi test.sh
#!/bin/bash
function no_input(){
echo "hello world"
}
"$@"
|
”$@” is what that makes the script file (.sh) be able to call methods
1
2
| $ ./test.sh no_input
hello world
|
- Add another method to file
1
2
3
4
5
6
7
8
9
10
11
12
13
| $ vi test.sh
#!/bin/bash
function with_input(){
echo "hello $1 how are you?"
}
function no_input(){
echo "hello world"
}
"$@"
|
- Call new method with input argument
1
2
| $ /test.sh with_input 'User'
hello User how are you?
|