Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 17-03-2009
Member
 
Join Date: Mar 2008
Posts: 264
BASH Programming Functions

As in almost any programming language, you can use functions to group pieces of code in a more logical way or practice the divine art of recursion.

Declaring a function is just a matter of writing function my_func { my_code }.

Calling a function is just like calling another program, you just write its name.

Eg.

Code:
 #!/bin/bash 
           function quit {
               exit
           }
           function hello {
               echo Hello!
           }
           hello
           quit
           echo foo

Lines 2-4 contain the 'quit' function. Lines 5-7 contain the 'hello' function If you are not absolutely sure about what this script does, please try it!.

Notice that a functions don't need to be declared in any specific order.

When running the script you'll notice that first: the function 'hello' is called, second the 'quit' function, and the program never reaches line 10.
Reply With Quote
  #2  
Old 17-03-2009
Member
 
Join Date: Mar 2008
Posts: 264
Re: BASH Programming Functions

Functions with parameters

Code:
#!/bin/bash 
                function quit {
                   exit
                }  
                function e {
                    echo $1 
                }  
                e Hello
                e World
                quit
                echo foo
This script is almost identically to the previous one. The main difference is the funcion 'e'. This function, prints the first argument it receives. Arguments, within funtions, are treated in the same manner as arguments given to the script.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "BASH Programming Functions"
Thread Thread Starter Forum Replies Last Post
Problems with object-oriented programming, classes, objects, functions Lakshmigopal Software Development 5 04-12-2010 11:01 AM
Socket programming: Is any new Programming Language? Kushan Software Development 3 14-11-2009 11:13 AM
Double Ampersand in bash script programming KuNaLBuntU Software Development 3 20-08-2009 05:43 PM
Bash script if then else help Unix'EM Software Development 3 18-08-2009 06:34 PM
JetBrains Introduces a New Programming Paradigm With its Meta Programming System JoeFrat Software Development 3 13-12-2008 01:49 PM


All times are GMT +5.5. The time now is 11:33 AM.