Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working actually in a shell script to monitor a server resources. I have a function and I want to know how can I call a second function inside the main one.

Example:

funct mainfunct(){

echo "Text to show here" **$secondfunct**

}

funct secondfunct(){
commands
}
share|improve this question
    
Write the name of the first function in the body of the second function ? – 123 Mar 4 at 15:42
1  
What shell is that? I don't recognize the "funct" part. – Jeff Schaller Mar 4 at 16:32
up vote 1 down vote accepted

In ksh or bash,

mainfunct() {
  echo "Text to show here" $(secondfunct)
}

secondfunct() {
  echo commands here
}

mainfunct

Generates the following:

Text to show here commands here

share|improve this answer
    
It should be noted that the $(secondfunct) here would expand to the words resulting from the split+glob operator applied to the standard output of secondfunct stripped of all trailing newline characters. – Stéphane Chazelas Mar 4 at 16:53
    
That's not limited to bash and ksh. That would work in any POSIX shell (and some non POSIX ones like ash or zsh (though zsh wouldn't do the glob part and not choke on NUL bytes)) – Stéphane Chazelas Mar 4 at 16:55
android@localhost:~/test$ cat fun.sh
function myname {
  echo "my name is raja"
}

function call {
  myname
}
call
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.