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

typeset -p & manpage on it are confusing w/rt funcs

3 views
Skip to first unread message

Linda Walsh

unread,
Jun 6, 2013, 6:48:09 PM6/6/13
to bug-bash

I wanted to test to see if a function was defined and looking at
typeset in the bash man page, I see
typeset ... The -p option will
display the attributes and values of each name. When -p is used
with name arguments, additional options are ignored. When -p is
supplied without name arguments, it will display the attributes
and values of all variables having the attributes specified by
the additional options. If no other options are supplied with
-p, declare will display the attributes and values of all shell
variables. The -f option will restrict the display to shell
functions. The -F option inhibits the display of function defi-
nitions;


ok ... so reading the above, how does "-f" and -F" tie in with
"-p" ?? If I use -f with -p does that limit it to functions only?
Er... what?

If I define local functions and vars:
> env -i bash --norc
bash-4.2$ PATH=.:/usr/bin:/bin
bash-4.2$ function fff () { echo "fff_func" ;}
bash-4.2$ aaa=eee
bash-4.2$ typeset -p |grep -P 'aaa|fff'
declare -- aaa="eee"
bash-4.2$ fff
fff_func

So why is 'aaa' displayed but not fff?

doesn't -p display all the names and their attributes?
If I wanted it to only work with functions...

Could I use -f?:

bash-4.2$ typeset -pf
fff ()
{
echo "fff_func"
}
bash-4.2$ typeset -fp
fff ()
{
echo "fff_func"
}
---
Nope. No attributes..
bash-4.2$ export fff
bash-4.2$ typeset -fp
fff ()
{
echo "fff_func"
}

Still no attributes:
$ typeset -p aaa
declare -- aaa="eee"
bash-4.2$ typeset -p fff
bash: typeset: fff: not found
-- "-p" print "aaa", but doesn't find the function.

But without a name give to "-p"
bash-4.2$ typeset -p |grep -P 'aaa|fff'
declare -- aaa="eee"
declare -x fff
----
In the dump of all, I see that fff is exported. Why don't
I see it when I specify it by name?
i.e. typeset -p fff:
$ typeset -p fff
bash: typeset: fff: not found

--- Seems like "-p" doesn't work consistently with functions?

Is that something that could be made consistent -- as it is it is
more than a little confusing... especially with the docs talking about
using -f/-F to include/exclude functions when giving names to -p (but it
seems like it doesn't work when you give names to -p, only in the
"no name"case.

I can sorta figure out what I want by looking at the status of typeset -f <func>
and sending the output to /dev/null, but that doesn't address the
confusing behavior or "-p" or how it is documented...???






Greg Wooledge

unread,
Jun 7, 2013, 8:15:50 AM6/7/13
to Linda Walsh, bug-bash
On Thu, Jun 06, 2013 at 03:48:09PM -0700, Linda Walsh wrote:
> I wanted to test to see if a function was defined

imadev:~$ unset foo
imadev:~$ foo=variable
imadev:~$ bar() { echo I am a function; }
imadev:~$ declare -f foo >/dev/null 2>&1 ; echo $?
1
imadev:~$ declare -f bar >/dev/null 2>&1 ; echo $?
0

> and looking at
> typeset in the bash man page, I see

imadev:~$ help typeset
typeset: typeset [-aAfFgilrtux] [-p] name[=value] ...
Set variable values and attributes.

Obsolete. See `help declare'.

But "typeset" does work in place of "declare" in the examples above.
They appear to be interchangeable, for now, in this particular case.

Linda Walsh

unread,
Jun 8, 2013, 9:44:03 PM6/8/13
to bug-bash


Greg Wooledge wrote:
> On Thu, Jun 06, 2013 at 03:48:09PM -0700, Linda Walsh wrote:
>> I wanted to test to see if a function was defined
>
> imadev:~$ declare -f foo >/dev/null 2>&1 ; echo $?
> 1
> imadev:~$ declare -f bar >/dev/null 2>&1 ; echo $?
> 0
> Obsolete. See `help declare'.
> But "typeset" does work in place of "declare" in the examples above.
> They appear to be interchangeable, for now, in this particular case.
-----
Yeah, I see that, I supposed I was looking for output from decl/typeset
from the -p switch showing just the funcname with any attrutes ( like export)..
just as it does for the variables... unless it is a local function and then I see
-- for the attributes.

The confusing part is having the -f and -F in the same paragraph as -p.

It looks like it is a paragraph mostly about 'p', and so you read
-p prints all names+attr Us9ing -f will restrict the display to shell functions
and -F inhibits them. --- When used with '-p'? no -- as it ealier says 'f'
can't be used with any other args if it is used with a name.

Seems like if I type declare -p 'funcname', I get ' typeset -p include
-bash: typeset: include: not found -- I tried to use it with -f to just see
functions:
typeset -fp include
-bash: typeset: include: not found
...

-p can't be used with -f, but the manpage puts them in the same paragraph like
they are related switches -- where -f/-F can be used to modify the output of
-p -- which doesn't seem to be the case.

That's the confusion, really... Just some unclarity in the words, I thought.

no biggie.

Chet Ramey

unread,
Jul 8, 2013, 5:05:36 PM7/8/13
to Linda Walsh, bug-bash, chet....@case.edu
On 6/6/13 6:48 PM, Linda Walsh wrote:
>
> I wanted to test to see if a function was defined and looking at
> typeset in the bash man page, I see
> typeset ... The -p option will
> display the attributes and values of each name. When -p is used
> with name arguments, additional options are ignored. When -p is
> supplied without name arguments, it will display the attributes
> and values of all variables having the attributes specified by
> the additional options. If no other options are supplied with
> -p, declare will display the attributes and values of all shell
> variables. The -f option will restrict the display to shell
> functions. The -F option inhibits the display of function defi-
> nitions;
>
>
> ok ... so reading the above, how does "-f" and -F" tie in with
> "-p" ?? If I use -f with -p does that limit it to functions only?

I think the original intent of the -p option was to have it interact with
-f and -F to limit each name argument to the function namespace, and to
display definitions and attributes. I will change the code and revise
the documentation to reflect that.

Chet

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU ch...@case.edu http://cnswww.cns.cwru.edu/~chet/

Linda Walsh

unread,
Jul 8, 2013, 5:28:26 PM7/8/13
to chet....@case.edu, bug-bash


Chet Ramey wrote:
> On 6/6/13 6:48 PM, Linda Walsh wrote:
>> I wanted to test to see if a function was defined and looking at
>> typeset in the bash man page, I see
>> typeset ... The -p option will
>> display the attributes and values of each name. When -p is used
>> with name arguments, additional options are ignored. When -p is
>> supplied without name arguments, it will display the attributes
>> and values of all variables having the attributes specified by
>> the additional options. If no other options are supplied with
>> -p, declare will display the attributes and values of all shell
>> variables. The -f option will restrict the display to shell
>> functions. The -F option inhibits the display of function defi-
>> nitions;
>>
>>
>> ok ... so reading the above, how does "-f" and -F" tie in with
>> "-p" ?? If I use -f with -p does that limit it to functions only?
>
> I think the original intent of the -p option was to have it interact with
> -f and -F to limit each name argument to the function namespace, and to
> display definitions and attributes. I will change the code and revise
> the documentation to reflect that.
---
Yeah, right now, if you specify -f or -F with -p, it's as though
-p is ignored, whereas typeset -p by itself seems to give the attributes of -F
(non-funcs). I don't know how much it's worth, but it seems that
w/o -f or -F, -p might display attributes&values of funcs+vars,
with -f, funcs only, and -F as it acts now by itself (i.e. attribs of func+vars).

Probably meant to do something orthogonal like that but maybe got
interrupted in the middle of implementation and the idea fell off "the stack"
so it never got completed....or at least I can see that happening to me...;-)

0 new messages