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...
how to write function?
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
how to write function?
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.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: how to write function?
Moved here from Newbie Questions
Re: how to write function?
#!/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.
Feel free to correct me if I'm trying to write in Spanish, French or German.
Re: how to write function?
Here's a very simple example of a bash function:
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:
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:
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.
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
Code: Select all
calculate 45 9
calculate 2 6
calculate 101 202
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
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
Arch
Dual GTX1070 8Gb
AMD Ryzen 1800X
32Gb RAM