# Return vs. Echo

**URL:** <https://community.unix.com/t/return-vs-echo/230210>\
**Category:** Shell Programming and Scripting\
**Created:** [February 3, 2009, 5:33pm UTC](https://community.unix.com/t/return-vs-echo/230210 "2009-02-03T17:33:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gagan8877](https://community.unix.com/letter_avatar/gagan8877/32/6_22734026db12803ebb3e059622ac3534.png) [@gagan8877](https://community.unix.com/u/gagan8877)\
**Post date:** [February 3, 2009, 5:33pm UTC](https://community.unix.com/t/return-vs-echo/230210/1 "2009-02-03T17:33:42Z")

</div>

In a Ksh functions, when you have both echo and return respectively - what does it do. E.g.

Func B ()  
{  
.....  
{  
.....  
echo $Varaible  
}  
Return 0  
}

Func A ()  
{  
$Var1 = Func B()  
....  
....  
}

Echo $Var1 produces the value of $Variable, so where did Return 0 go? What if echo was not there then would would be the result?

Thanks  
-Novice

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [February 3, 2009, 5:44pm UTC](https://community.unix.com/t/return-vs-echo/230210/2 "2009-02-03T17:44:03Z")

</div>

There is no similarity between them: echo prints its arguments to stdout; return sets a return code which may be tested directly (by if or a control operator, && or ||) or indirectly in the contents of $?.

---

<div class="post-metadata">

**Author:** ![gagan8877](https://community.unix.com/letter_avatar/gagan8877/32/6_22734026db12803ebb3e059622ac3534.png) [@gagan8877](https://community.unix.com/u/gagan8877)\
**Post date:** [February 4, 2009, 10:01am UTC](https://community.unix.com/t/return-vs-echo/230210/3 "2009-02-04T10:01:54Z")

</div>

Hi cfajohnson

Thanks for the reply. In the code below GetData() calls get\_primary and is\_primary functions

GetData()  
{

prim\_mach=`get_primary`  
gprim\_mach=`is_primary`

echo $prim\_mach  
echo $gprim\_mach  
}

-------------------

is\_primary()  
{  
TFILE=$TEMPDIR/tempFile.$$  
this\_server=`uname -n`  
dbaccess \<\< PRIM \> /dev/null 2\>&1  
database sscDB;  
unload to $TFILE select s\_status from serverstbl where s\_name = "$this\_server";  
PRIM  
[-f $TFILE] &&  
{  
status=`cat $TFILE | awk -F"|" '{ print $1 }'`  
rm $TFILE  
["$status" = "P"] && return 0  
}  
return 1  
}

----------------

TFILE=$TEMPDIR/tempFile.$$  
dbaccess \<\< GPRIMM \> /dev/null 2\>&1  
database sscDB;  
unload to $TFILE select s\_name from serverstbl where s\_status = "P";  
GPRIMM  
[-f $TFILE] &&  
{  
status=`cat $TFILE | awk -F"|" '{ print $1 }'`  
rm $TFILE  
echo $status  
}  
return 0  
}

----------------------------------------------------  
echo $prim\_mach prints the column value - echo is the only command I see in the code that can send the output to $prim\_mach variable in GetData(). So echo sent value to $prim\_mach. But it returned 0 too. How do I see the 0? I thought the variable $prim\_mach will have 0 in it, but it showed the echoed value.

echo $gprim\_mach prints nothing, becasue there is no echo. But it did return 0 or 1 depending upon the condition. How can I see that?

Thanks.

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [February 4, 2009, 1:39pm UTC](https://community.unix.com/t/return-vs-echo/230210/4 "2009-02-04T13:39:30Z")

</div>

> [@gagan8877](#):
>
> echo $gprim\_mach prints nothing, becasue there is no echo. But it did return 0 or 1 depending upon the condition. How can I see that?

I repeat:

> [@](#):
>
> in the contents of $?.

---

<div class="post-metadata">

**Author:** ![gagan8877](https://community.unix.com/letter_avatar/gagan8877/32/6_22734026db12803ebb3e059622ac3534.png) [@gagan8877](https://community.unix.com/u/gagan8877)\
**Post date:** [February 5, 2009, 10:04am UTC](https://community.unix.com/t/return-vs-echo/230210/5 "2009-02-05T10:04:03Z")

</div>

Can you please show me the syntax of how to check the exit code in getdata() ?

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [February 5, 2009, 1:21pm UTC](https://community.unix.com/t/return-vs-echo/230210/6 "2009-02-05T13:21:59Z")

</div>

> [@gagan8877](#):
>
> Can you please show me the syntax of how to check the exit code in getdata() ?

```plaintext
getdata
echo $?

```

If you want to check the error status of commands within getdata:

```plaintext
GetData()
{

prim_mach=`get_primary`
echo $?
gprim_mach=`is_primary`
echo $?

echo $prim_mach
echo $?
echo $gprim_mach
echo $?
}

```

Normally you would use a conditional expression and do something different depending on the exit code of the command.

---

<div class="post-metadata">

**Author:** ![gagan8877](https://community.unix.com/letter_avatar/gagan8877/32/6_22734026db12803ebb3e059622ac3534.png) [@gagan8877](https://community.unix.com/u/gagan8877)\
**Post date:** [February 5, 2009, 4:22pm UTC](https://community.unix.com/t/return-vs-echo/230210/7 "2009-02-05T16:22:37Z")

</div>

Fantastic. Thanks a lot cfajohnson.
