Posts Linux bash call functions
Post
Cancel

Linux bash call functions

  • Create an empty file with .sh extension
1
$ touch test.sh
  • 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

  • Make file executable
1
$ chmod u+x test.sh
  • Call method
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?
This post is licensed under CC BY 4.0 by the author.