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 are environment variables?

2 Answers  


In a file , how to retrieve the lines which are the multiples of 50 ? like 50,100,150th lines etc.

9 Answers   Amazon,


Explain about stdin, stdout and stderr?

1 Answers  


write a shell script to emulate the Id command of PRIMOS which lists files and directories. It list files first with a header FILES and then directories with a header DIRECTORIES. This command has several options. The main ones are. -file select files only -dir select directories only -reverse sort in reverse order -no_header put no header on the output -brief output the header only -size display the size of each file -help display Id´s syntax and options.

1 Answers  


Is bash a shell script?

1 Answers  


Write down the syntax of "for " loop

1 Answers  


What is the use of "shift" command in passing parameters?

1 Answers  


What is Path variable?What is its use?

1 Answers  


Suppose you execute a command using exec, what will be the status of your current process in the shell?

1 Answers  


I want to upload a file to remote server through ftp daily.Can anyone suggest how to make a shell script for that.I hv credentials for that ftp

1 Answers  


What is the difference between running a script as ./scriptname.sh and sh scriptname.sh

1 Answers  


What is the need of including script interpreter in your shell script?

2 Answers  


Categories