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  


how to separate the even and odd number generated from one file to two separate file i.e. even numbers in file1.txt and odd numbers in file2.txt

4 Answers   Infosys,


What is a shell in operating system?

0 Answers  


c program to display the information of given file similar to givan by the unix or linux command ls -l

0 Answers   IBM,


What is the difference between break and continue commands?

0 Answers  


How do I stop script errors?

0 Answers  


Is powershell a bash?

0 Answers  


What are the additional egrep symbols?

2 Answers  


Write down the syntax for all the loops in shell scripting.

0 Answers  


How to redirect both standard output and standard error to the same location?

0 Answers  


Using set -A write a script to print the output of the ls command in 5 columns with two spaces between each column. Pretend that ls does not have multicolumn output.

0 Answers  


Print the 10th line without using tail and head command.

0 Answers  


Categories