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 does egrep mean?

0 Answers  


What is the crontab?

0 Answers  


Explore about environment variables?

0 Answers  


What is the use of "$#" in shell scripting?

0 Answers  


I want to read all input to the command from file1 direct all output to file2 and error to file 3, how can I achieve this?

1 Answers  


how did u debugging in unix/ linux platform ??( project Related)

2 Answers   Symphony,


How to add some content in any file at some desired location without using VI or some other editor in UNIX

2 Answers  


Create a bash shell script that reads in a number from the user. If the number is 1, print out the date. If the number is 2, list the files in the current directory. If the number is 3, print out who is currently logged onto the system. If the number is anything else, print out an error message and exit. Name this script "various.sh"

4 Answers  


Why do we write bin bash in shell scripts?

0 Answers  


Is shell scripting useful?

0 Answers  


wats the she bang statment with syntax?

3 Answers  


I want to monitor a continuously updating log file, what command can be used to most efficiently achieve this?

0 Answers  


Categories