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

how to search for vowels a,e,i,o,u appearing in the same sequence in a file

4 Answers   Amazon,


How does shell scripting work?

0 Answers  


write a program to display all the files from the current directory which are created in particular month

6 Answers  


c program to implement unix/linux command to block the signal ctrl-c and ctrl-\ signal during the execution ls -l|wc -l

0 Answers  


Write down the syntax of "for " loop

0 Answers  






What is in a script?

0 Answers  


c program which accept one argument as a directory name and prints all the file name along with its inode number and total count of the file in directory

1 Answers  


What is @echo off?

0 Answers  


Explain how you Automate your application using Shell scripting.

0 Answers   MAHINDRA,


How do you rename the files(*.sh) with file names containing space in it?for example "interview question.sh" needs to rename to "interview-question.sh". Appreciate your inputs.Thanks.

5 Answers   Wells Fargo,


What is the difference between grep and egrep?

0 Answers  


Write a command sequence to find the count of each word?

0 Answers  


Categories