How do I create Simple Bash Functions

How do I create Simple Bash Functions

In this tutorial I will show you, how to create simple Bash functions, so how do I create Simple Bash Functions?

The functions and the aliases are very useful.

Both, the aliases and the functions have the same purpose. Making the terminal usage easier. When you cannot alias something, function it. The functions get the arguments passed.

Syntax: Function_Name () { command ; }

Example: test () { echo ‘it works!’ ; }

To call the function, type it’s name in the terminal.

$ test

Passing arguments:

  • $1  –  First Argument:
  • $2  –  Second Argument:
  • $3  –  Third Argument:
  • $@ and $* Store all the positional arguments as one single string

How to pass arguments to functions or shell scripts, function examples:

1: hello1 () { echo “Hello $1!” ; }

$ hello1 Mike
Hello Mike!

2: hello2 () { echo “Hello $1 and $2!” ; }

$ hello2 Mike John
Hello Mike and John!

3: hello3 () { echo “Hello $@!” ; }

$ hello3 Mike John Dave
Hello Mike John Dave!

Was this Tutorial helpful? Help others share on Facebook, Twitter, and Google Plus!

Enjoyed this video?
How do I create Simple Bash Functions
"No Thanks. Please Close This Box!"