[prog] Returning a string from a function, in bash

Sue Stones suzo at spin.net.au
Sat Apr 17 09:09:43 EST 2004

On Friday 16 Apr 2004 5:26 am, Riccarda Cassini wrote:
> Hi everyone,
> I'm trying to learn some shell scripting (in bash), and, at the risk
> of outing myself as a total newbie, I'd like to get some advice on the
> following question. Hope it's not too basic for this list.

Nothing is too basic for this list.  A large proportion of the questions are 
people's firt attempt at a language, or indeed programming itself.

> This seems to work, so I basically could be happy with it. However, I'm
> a little unsure as to whether this is the way one's supposed to do it.
> Do I really have to use echo and backticks?  

What the backticks do really is present the output from a program (which is 
outside of the current script) as arguments.  So you do need to have 
something to pass them as arguements to ... in this case you have used echo 
to print them.

You originally said that you wanted to return the result as some variable.  
All you have done here is to print out the result of the program that you 
have run, not assign it to a variable.  To do that you need to create a shell 
variable (this is done differently in diferent shells by the way). Shell 
variable are words that are fully capitalised.

$ echo `date`				// the output of date is passed to echo as arguments
Sat Apr 17 09:06:06 EST 2004		// prints the arguments, but thats all

$ DATE=`date` 	// the output of date is assigned to the shell variable DATE
$ echo $DATE	// the infomation is still there for you to do use
Sat Apr 17 09:02:30 EST 2004


More information about the Programming mailing list