O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

19.2. Using functions in a script

Now we have created a function let’s see it being used in a script.

						$ pg func1 
#!/bin/sh 
# func1 
hello () 
{
echo "Hello there today’s date is `date`" 
} 

echo "now going to the function hello" 
hello 

echo "back from the function" 

When the above script is run, we get:

						$ func1 
now going to the function hello 
Hello there today’s date is Sun Jun 6 10:46:59 GMT 1999 
back from the function
					

In the above example, the function is declared at the top of the script. To reference the function we called it by its name which was ‘hello’. When the function has finished, control returns to the next statement following the function call, which is the echo statement ‘back from the function’.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required