≡ Menu

Howto: Call Shell Script function In echo statement

I need to call the function called foo() using echo command in the same statement. How do I all or invoke the function using the echo statement?

You can define a function using the following syntax:

foo(){
    date
}

Now you can call the function foo() using the echo statement as follows:

echo "Today is $(foo)"

Sample outputs:

Today is Sat Jan 22 00:27:26 IST 2011

A sample shell script to display system info:

#!/bin/bash
findcpu(){
	grep 'model name' /proc/cpuinfo  | uniq | awk -F':' '{ print $2}'
}
 
findkernelversion(){
	uname -mrs
}
 
totalmem(){
	grep -i 'memtotal' /proc/meminfo | awk -F':' '{ print $2}'
}
 
echo "CPU Type : $(findcpu)"
echo "Kernel version : $(findkernelversion)"
echo "Total memory : $(totalmem)"

See also:

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:



{ 4 comments… add one }
  • amke April 22, 2011, 4:11 pm

    Cool, exactly what I was looking for.
    Thanx!

  • Vivek May 1, 2012, 12:41 pm

    Good one, Helpful Thanks!!

  • hrg February 4, 2013, 11:14 am

    Wonderful, thanks!

  • Santanu October 9, 2015, 9:48 am

    cool.. short and good…thx

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


   Tagged with: , , , , , , , , , , , , ,