Create Simple Bash Functions

The functions and the aliases are very usefull.

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!

Tagged with: , , , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!