How to declare functions in unix shell script?

Answers were Sorted based on User's Feedback



How to declare functions in unix shell script?..

Answer / sujay kumar

First method:

function function_name
{
}

Second Method:

function_name()
{
}

For both methods, the invocation of function would be in
the same format only.

Invocation:

function_name

Is This Answer Correct ?    62 Yes 12 No

How to declare functions in unix shell script?..

Answer / prakash

function Dissplay () {

echo "This is how we define function"

}


Display # Invoking Function

## Function should be defined before we invoke it

Is This Answer Correct ?    67 Yes 26 No

How to declare functions in unix shell script?..

Answer / nandish

we can pass the parameter also
ex: function fun_name () {

# write block here
}
invoke function as

fun_name parameter1 parameter2 ...

Is This Answer Correct ?    9 Yes 3 No

How to declare functions in unix shell script?..

Answer / pitambar mishra

### Note : Function always returns a value.
### This script is for adding two numbers.
### Script name : add.ksh

#!/bin/ksh

sum() ### Defining sum function
{
echo enter 2 no :
read num1 num2
echo sum=$(( num1 + num2 ))
}
sum ### Invoking sum function

To execute it :
ksh add.ksh

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Shell Script Interview Questions

What is console line?

0 Answers  


write a non recursive shell script that accepts any number of arguments and prints them in the reverse order

3 Answers  


How do I run a .sh file on mac?

0 Answers  


what do u mean by $#,$* in unix programming?

5 Answers   Convergys, TCS,


How can we find the process name from its process id?

0 Answers  






what is this line in the shell script do ?#!/bin/ksh

5 Answers  


Write a shell script to get current date, time, user name and current working directory.

0 Answers  


What is the equivalent of a file shortcut that we have a window on a linux system?

0 Answers  


How to calculate the number of passed arguments?

0 Answers  


What is bourne shell scripting?

0 Answers  


Create a bash shell script to sort and then uniq the file from the command line & store it to a new file and output the results to the screen. Name this script "sortAndUniq.sh"

5 Answers  


What is the difference between a 'thread' and a 'process'?

3 Answers  


Categories