Create Simple Bash Functions

The functions and the aliases are very usefull.

Watch Free Movies

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!

Scroll to Top