Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

type identify a function

13 views
Skip to first unread message

Marek Novotny

unread,
Jan 15, 2017, 9:56:48 PM1/15/17
to
I'm confused by this command, type.

type ls cat do
ls is aliased to `ls --color=auto'
cat is /usr/bin/cat
do is a shell keyword

I'm supposed to be able to identify a function with the *type* command.
But I don't understand this. Is the function a file I can create? Do I
create a shell script, with functions inside of it, and use type against
the file name? That doesn't seem to identify functions inside the file.
How do I create a situation in which I can see type return a function?

--
Marek Novotny
https://github.com/marek-novotny

Bit Twister

unread,
Jan 15, 2017, 10:45:29 PM1/15/17
to
On Sun, 15 Jan 2017 20:56:40 -0600, Marek Novotny wrote:
> I'm confused by this command, type.
>
> type ls cat do
> ls is aliased to `ls --color=auto'
> cat is /usr/bin/cat
> do is a shell keyword
>
> I'm supposed to be able to identify a function with the *type* command.

Yep, works for me.

> But I don't understand this. Is the function a file I can create?

You can but may not be able to run it. :(

> Do I
> create a shell script, with functions inside of it, and use type against
> the file name?

Nope, just use the function name to be checked.

> That doesn't seem to identify functions inside the file.

Hmmm, works for me.

> How do I create a situation in which I can see type return a function?

I would guess something like
#!/bin/bash

function me ()
{
echo "from me (type me)"
}

type do
me

Bit Twister

unread,
Jan 15, 2017, 10:48:56 PM1/15/17
to
Oops. need a $ in that echo statement

#!/bin/bash

function me ()
{
echo "from me $(type me)"
}

type do
me

Marek Novotny

unread,
Jan 15, 2017, 11:11:53 PM1/15/17
to
I'm still missing this...

Do you mean this...

Create a bash shell script with the following:

#!/bin/bash

function me ()
{
echo "from me $(type me)"
}

# end

From the command line:

$ type do

Is the name of the file, do?

Marek Novotny

unread,
Jan 15, 2017, 11:24:05 PM1/15/17
to
[marek@thinkpad ~]$ ./sample
do is a shell keyword
from my script: me is a function
me ()
{
echo "from my script: $(type me)"
}
[marek@thinkpad ~]$

Okay, I got this after all.

Does this have to be called from inside a script? I have a book and it
looks like it shows me this being done from the command line some how.
That's what's confusing me.

Bit Twister

unread,
Jan 15, 2017, 11:29:06 PM1/15/17
to
On Sun, 15 Jan 2017 22:23:57 -0600, Marek Novotny wrote:
>
> Okay, I got this after all.
>
> Does this have to be called from inside a script? I have a book and it
> looks like it shows me this being done from the command line some how.
> That's what's confusing me.

That surprises me. The book is just like any other boot explaining
commands.

How you use them in a script is up to you.

I gave the example because I assumed you were wanting to know if a
function was defined within a script.

Marek Novotny

unread,
Jan 15, 2017, 11:35:50 PM1/15/17
to
On 2017-01-16, Bit Twister <BitTw...@mouse-potato.com> wrote:
I have a book and it shows type being used on the command line.

$ type ls cat do

ls is aliased to `ls --color=auto'
cat is hashed (/usr/bin/cat)
do is a shell keyword

But in the book there is a forth item and it shows it spit out a
function.

So like so:

[marek@thinkpad ~]$ type ls cat do me
ls is aliased to `ls --color=auto'
cat is hashed (/usr/bin/cat)
do is a shell keyword
bash: type: me: not found
[marek@thinkpad ~]$

me being the function, called exactly same way as the other three
directly from the command line, not a script and in the example, it
spits out a function. How?

Bit Twister

unread,
Jan 16, 2017, 12:02:06 AM1/16/17
to
On Sun, 15 Jan 2017 22:35:42 -0600, Marek Novotny wrote:
>
> [marek@thinkpad ~]$ type ls cat do me
> ls is aliased to `ls --color=auto'
> cat is hashed (/usr/bin/cat)
> do is a shell keyword
> bash: type: me: not found
> [marek@thinkpad ~]$
>
> me being the function, called exactly same way as the other three
> directly from the command line, not a script and in the example, it
> spits out a function. How?

"type" is a bash builtin. When you "type something" "type" runs
through its process database for alias, function, $PATH called something.
First item found is what is returned unless you use an argument.

If your were to define the me function within the current process,
me should show up.

Marek Novotny

unread,
Jan 16, 2017, 12:12:03 AM1/16/17
to
On 2017-01-16, Bit Twister <BitTw...@mouse-potato.com> wrote:
I hear ya...

Here is the page...

http://imgur.com/L8pSMsl

It could have been a little more clear. The next page says, "You can
also see that the function definition is printed when we stumble across
a function when using type.

That to me sounds like someone at the command line, using: type
something and running across a function... Essentially, this is much
more deliberate than that.

Janis Papanagnou

unread,
Jan 16, 2017, 5:38:02 AM1/16/17
to
I get the same output as you; because I have no program/script/alias
called "me". ls, cat, do, are on the other hand standard and available,
so we get information about it. Does the course of your book maybe
define a function 'me' as example, and have you forgot to incorporate
it into your shell session? That would at least explain the effect you
observe. Define a shell function, like me () { :; } and retry
the type command (in the same shell window).

Janis

Thomas 'PointedEars' Lahn

unread,
Jan 16, 2017, 7:31:30 AM1/16/17
to
Bit Twister wrote:
^^^^^^^^^^^
Get a real name.

> On Sun, 15 Jan 2017 20:56:40 -0600, Marek Novotny wrote:
>> I'm confused by this command, type.
>>
>> type ls cat do
>> ls is aliased to `ls --color=auto'
>> cat is /usr/bin/cat
>> do is a shell keyword
>>
>> I'm supposed to be able to identify a function with the *type* command.
>
> Yep, works for me.

Of course, one should use the “-t” option and compare against the string
result, which would be "function" if it were a function. One could
additionally use the “-f” option to ignore built-in functions.

RTFM.

>> But I don't understand this. Is the function a file I can create?
>
> You can but may not be able to run it. :(

Nonsense. A function exists only in memory while the shell is being
executed. You can write a file (shell script) declaring a function and
source that (“source” or “.” command) to import the function into the
current shell context.

>> Do I create a shell script, with functions inside of it, and use type
>> against the file name?
>
> Nope, just use the function name to be checked.
>
>> That doesn't seem to identify functions inside the file.
>
> Hmmm, works for me.

You need to read more carefully.

>> How do I create a situation in which I can see type return a function?
>
> I would guess something like
> #!/bin/bash
>
> function me ()
> {
> echo "from me (type me)"
> }
>
> type do
> me

See above.

The OP’s question was whether it is possible to determine if a file declares
a function. In theory, there is, but without source code parsing only if
you source the file. Then you can check before and after sourcing the file
whether a function of a certain name exists. (But that carries with it the
risk of executing other code if the file does not serve only as a function
library.)

If not before but after, then something in that file or something sourced by
that file declared the function.

If neither before nor after, then the file does not declare the function or
execution does not reach a point where the function is declared.

If before, then a function with that name exists already, and sourcing the
file could only overwrite it.

Most interestingly, if you find that there is no built-in function of a
certain name before, you can declare the function in the same script or
source a file as a polyfill for the builtin, and avoid declaring/sourcing it
if the more efficient builtin appears to be available:

[ "$(type -t foo)" == "function" ] || foo () {
echo 42
}

Likewise for features that are not universally available, e.g.:

type seq >/dev/null 2>&1 || seq () {
# seq polyfill
}

[Careful, though: there are varying implementations of seq; when it doubt,
stick to the standard implementation and invocation. OTOH, the features
that a seq(1) implementation supports can be easily tested with dummy data.]


For this article I learned that one can undeclare a function with

unset -f "$name"

While usually advertized as a Bash feature, it is actually a standard one:

<http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset>

--
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

Marek Novotny

unread,
Jan 16, 2017, 8:36:11 AM1/16/17
to
Thank you!

$ me () { echo hello ; echo hello ; }

[marek@thinkpad ~]$ type me
me is a function
me ()
{
echo hello;
echo hello
}

I see it now in the shell directly.

Marek Novotny

unread,
Jan 16, 2017, 8:37:45 AM1/16/17
to
I have only ever sourced for a function inside a shell script once or
twice to see it working. I will experiment with it again and try your
suggestion. Thanks for the help!
0 new messages