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’.

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.