Why some user functions don't get recognised by bash?

Posted by strapakowsky on Ask Ubuntu See other posts from Ask Ubuntu or by strapakowsky
Published on 2012-06-03T23:32:27Z Indexed on 2012/06/04 4:47 UTC
Read the original article Hit count: 365

Filed under:
|

I can define a function like:

myfunction () { ls -R "$1" ; }

And then

myfunction .

just works.

But if I do

echo "myfunction ." | sh
echo "myfunction ." | bash

the messages are:

sh: myfunction: not found  
bash: line 1: myfunction: command not found

Why? And how can I call a function that comes from a string if not by piping it to sh or bash?

I know there is this command source, but I am confused of when I should use source and when sh or bash. Also, I cannot pipe through source. To add to confusion, there is this command . that seems to have nothing to do with the "." that means "current directory".

© Ask Ubuntu or respective owner

Related posts about command-line

Related posts about bash