how to write function?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
kaykav

how to write function?

Post by kaykav »

Hi
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 LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Oscar799
Level 20
Level 20
Posts: 10411
Joined: Tue Aug 11, 2009 9:21 am
Location: United Kingdom

Re: how to write function?

Post by Oscar799 »

Moved here from Newbie Questions
Image
AlbertP
Level 16
Level 16
Posts: 6701
Joined: Sun Jan 30, 2011 12:38 pm
Location: Utrecht, The Netherlands

Re: how to write function?

Post by AlbertP »

#!/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
Image
Feel free to correct me if I'm trying to write in Spanish, French or German.
User avatar
Roken
Level 5
Level 5
Posts: 737
Joined: Fri Nov 19, 2010 4:55 pm
Location: Newport, S Wales

Re: how to write function?

Post by Roken »

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

Code: Select all

#!/bin/bash

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

calculate $1 $2
exit
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))
	fi
	if [ ! -z 2 ] && [ ! -z 6 ]; then
		echo "The result of 2 plus 6 is " $((2 + 6))
	fi
	if [ ! -z 101 ] && [ ! -z 102 ]; then
		echo "The result of 101 plus 102 is " $((101 + 102))
	fi
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 5.12.14-zen1-1-zen, XFCE
Arch
Dual GTX1070 8Gb
AMD Ryzen 1800X
32Gb RAM
Locked

Return to “Scripts & Bash”