skip to main content

SAP NetWeaver Newbie

Functions in UNIX Shell

Functions in any programming language help you break down the script into logical subsections. UNIX shell lets you use functions.

Let's look at an example function called hello. Here is how we define hello function:
[user19@localhost ~]$ hello() {
> echo hello. please enter your nme
> read name
> echo welcome $name
> }

To call function hello, simply call it by its name:
[user19@localhost ~]$ hello
hello. please enter your nme
welcome user1

You can list the functions that you have defined using set command.
[user19@localhost ~]$set

hello ()
    echo hello. please enter your nme;
    read name;
    echo welcome $name

To undefine the function, use unset command:
[user19@localhost ~]$ unset hello

Functions are stored in memory and therefore are faster than a regular script. Any script or program with the same name as the function will have lesser precedence.

Let's see how we can use a function in a script. Let's define the function in a separate script called and use it in

[user19@localhost ~]$ cat
hello() {
echo Please enter your name
read name
echo Hi $name

[user19@localhost ~]$ cat
echo am going to call a function
echo before calling load the func
. ./
echo calling

Now, when we run

[user19@localhost ~]$ sh
am going to call a function
before calling load the func
Please enter your name
Hi user1

No comments:

Post a Comment

Email Subscription

Get every new post into your inbox by subscribing us.

Want a reason to subscribe?
1. This sitemap might convince you to subscribe.
2. We do not misuse email IDs. We respect privacy.

© 2008 - 2017 sapnwnewbie. All rights reserved.