Operating System - HP-UX
1833691 Members
5008 Online
110062 Solutions
New Discussion

Display code of a shell function?

 
SOLVED
Go to solution
Christian Deutsch_1
Esteemed Contributor

Display code of a shell function?

Hi folks,

I define a function in my shell (/usr/bin/sh):

x()
{
exit;
}

Is there a way for me to look and see how x was defined? If I use:

% whence x

I get:

x

But I don't see what's inside function x. Does anyone know of a way to display what's inside x?

Hope someone has a good idea about this. I couldn't find anything documented (maybe I didn't look in the right places?).

Thanks, Christian
Yeshua loves you!
7 REPLIES 7
Muthukumar_5
Honored Contributor

Re: Display code of a shell function?

whence command is an utility to check alias informations there in korn shell. It is ksh's efficient feature.


Example:

alias test='ls -l | grep "test"'

whence test
it will say that it is an alias to ls -l | grep "test"

There is a default alias to whence -v there as

alias type='whence -v'

so try as type test too

To get function information then,


alias fun='fun() { uname -a;hostname; }; fun'

It will give what did you expect that using,


whence -v fun
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Display code of a shell function?

You can know whence man information on ksh man page..

whence [-pv] name ...
For each name, indicate how it would be interpreted if
used as a command name. The -v option produces a more
verbose report. The -p option does a path search for
name even if name is an alias, a function, or a
reserved word.

http://www.robelle.com/library/smugbook/whence.html
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Display code of a shell function?

To view the code of function which is define on shell only then,

typeset +f

$ fun()
> {
> hostname
> uname -a
> }
$ fun
<...>

$ typeset +f

will give all function code there.

Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor
Solution

Re: Display code of a shell function?

To get only that function information then,

typeset -f

It will give informations / code of a function there.

See ksh man page for Funtions part ( search for "Functions" )



Easy to suggest when don't know about the problem!
Christian Deutsch_1
Esteemed Contributor

Re: Display code of a shell function?

Dear Muthukumar,

Thanks for your hints.

1. I am using /usr/bin/sh, not korn shell. ok there are not so many big differences
2. whence -v just tells me it's a function. ok that's nice to know but it doesn't tell me what's inside the function
3. typeset +f tells me which functions there are. ok that's nice to know but it doesn't tell me what's inside the functions

Kind regards, Christian
Yeshua loves you!
Christian Deutsch_1
Esteemed Contributor

Re: Display code of a shell function?

Dear Muthukumar,

ok all of your hints were useful and the last one was just what I wanted.

Thanks for your help, that's great!

Christian
Yeshua loves you!
Christian Deutsch_1
Esteemed Contributor

Re: Display code of a shell function?

.
Yeshua loves you!