LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-25-2010, 10:24 AM   #1
zizou86
LQ Newbie
 
Registered: Jan 2010
Posts: 16

Rep: Reputation: 0
Prevent echo as a return value?


Hi,

I have always encountered this problem in ubuntu bash shell scripts that echo command in a function will be treated as a return value when used in a function.

e.g.
Code:
function abc
{
   echo "xyz"
}

returnVal=$(abc)
echo "Return value = $returnVal"
This would give the output

Return value = xyz

However, if I used:

Code:
function abc
{
   echo "xyz"
}

abc
The output would simply be xyz.

Hence the echo seems to function as a "return" command when used in a function with a return value.

Any workaround to this?
 
Old 01-25-2010, 10:41 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
echo writes to stdout. In the first case the var=$(<function that runs echo) has the effect of capturing the stdout and assigning it to $var. In the second case stdout is not captured so appears on screen.

These are not return values; return values are set by bash' return command and accessed by the special variable "$?" (without the quotes).

Last edited by catkin; 01-25-2010 at 10:42 AM. Reason: Typographical SNAFU
 
Old 01-25-2010, 10:57 AM   #3
zizou86
LQ Newbie
 
Registered: Jan 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Hi,

My apologies for the wrong usage of terms. Is there any way that I can prevent the echo in the function from being assigned to a variable other? I would like the echo to echo to stdout and nowhere else.

Thank you.

Regards.
 
Old 01-25-2010, 11:48 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by zizou86 View Post
Is there any way that I can prevent the echo in the function from being assigned to a variable other? I would like the echo to echo to stdout and nowhere else.
Doesn't your second example do exactly that?

If you want some stuff echoed in the function to end up in a variable and some to end up going to the terminal you can redirect the terminal echoes to /dev/tty. Example (not using a function but illustrates the technique):
Code:
c:~$ var=$(echo foo; echo bar > /dev/tty)
bar
c:~$ echo $var
foo
 
Old 01-25-2010, 12:00 PM   #5
zizou86
LQ Newbie
 
Registered: Jan 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Great thanks alot catkin. It worked great.

What I wanted was more to prevent the echo from going to the variable then anything.

Thanks a great deal.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What exactly does "echo $0" return? kenneho Programming 8 01-19-2010 01:21 AM
"echo hello > /dev/ttyS0" does not return. rohitkugve Programming 2 01-11-2010 12:48 PM
Printing the return value using 'echo $?' command wikiban Linux - Newbie 1 08-03-2009 02:16 PM
How do I prevent echo eating spaces? essdeeay Linux - General 3 11-01-2005 03:11 PM
Prevent Terminal Echo Using Python (for Passwords) taylor_venable Programming 4 07-05-2005 11:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration