how to write function?

Forum rules
Before you post please read how to get help
Level 3
Level 3
Posts: 148
Joined: Tue Jan 06, 2009 12:09 am
Location: crossville tn

how to write function?

Postby kaykav » Thu Oct 20, 2011 3:22 pm

I have been trying to learn function writing for approx. two years now. I must have read hundreds or more articles and how-tos. I'm having trouble understanding the whole concept.
My question is: Do I have to write a shell script (#! /bin/bash) to create a function? Or? Also I can't see much difference betwen shell scripting and funtion writing. I'm not even sure I'm
phrasing this correctly. Non-the-less ,Do I have to begin creating a function with #!/bin/bash? Thank you...
Last edited by Oscar799 on Fri Oct 21, 2011 3:46 am, edited 1 time in total.
Reason: Edited title to make it clear it is a question not a howto guide

User avatar
Level 19
Level 19
Posts: 9698
Joined: Tue Aug 11, 2009 9:21 am
Location: United Kingdom

Re: how to write function?

Postby Oscar799 » Fri Oct 21, 2011 3:47 am

Moved here from Newbie Questions

Level 16
Level 16
Posts: 6715
Joined: Sun Jan 30, 2011 12:38 pm
Location: Utrecht, The Netherlands

Re: how to write function?

Postby AlbertP » Sat Oct 22, 2011 11:49 am

#!/bin/bash or #!/bin/sh marks the beginning of a shell script. You can write a function somewhere within the script. Note that I have never used functions inside Bash scripts.
Registered Linux User #528502
Feel free to correct me if I'm trying to write in Spanish, French or German.

User avatar
Level 5
Level 5
Posts: 741
Joined: Fri Nov 19, 2010 4:55 pm
Location: Warrington, UK

Re: how to write function?

Postby Roken » Sat Oct 22, 2011 2:56 pm

Here's a very simple example of a bash function:

Code: Select all


calculate() {
   if [ ! -z $1 ] && [ ! -z $2 ]; then
      echo "The result of $1 plus $2 is " $(($1 + $2))

calculate $1 $2

The actual function is cacluate, which takes two parameters, tests to make sure they are there, adds them together and outputs the result. The penultimate line simply calls the function using the two parameters passed to the script, but you could just as easily replace it with:

Code: Select all

calculate 45 9
calculate 2 6
calculate 101 202

and so on, to add up each pair of numbers that you pass to it. Really, all a function is is a mechanism to stop you having to repeat the same codeblock throughout your script. The three calculate lines shown above would expand internally to:

Code: Select all

   if [ ! -z 45 ] && [ ! -z 9 ]; then
      echo "The result of 45 plus 9 is " $((45 + 9))
   if [ ! -z 2 ] && [ ! -z 6 ]; then
      echo "The result of 2 plus 6 is " $((2 + 6))
   if [ ! -z 101 ] && [ ! -z 102 ]; then
      echo "The result of 101 plus 102 is " $((101 + 102))

so you see how much less typing is required for reusable code blocks by using a function.

Just to add, this really is a very simplistic example and you would be unlikely to use a function to do this, but the real power is for complex code blocks. Think of the code between the curly braces in the function as it's own script within a script,which can be as simple or as complex as necessary, and you get the idea.

EDIT: If you are familiar with BASIC programming, functions are the same as BASIC PROCEDURES.
Kernel Linux Tex 4.8.13-1-ARCH (64 bit) xfce , burg
GTX680 4Gb
AMD Phenom II X4 (965BE) @ 3.6 Ghz

Return to “Scripts & Bash”

Who is online

Users browsing this forum: No registered users and 2 guests