[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: `declare -f "a="' fails unnecessarily

From: Dale R. Worley
Subject: Re: `declare -f "a="' fails unnecessarily
Date: Sun, 04 Dec 2022 15:47:03 -0500

Emanuele Torre <torreemanuele6@gmail.com> writes:
> `declare -f "something="' fails with the following error:
>     $ declare -f 'a=x'
>     bash: declare: cannot use `-f' to make functions

> That error is not very useful. Bash makes `declare -f' fail with that
> error when an argument looks like an assignment.

It's an interesting mess.  Looking at the definition of "declare", the
"=" is used to separate the name from the value being assigned to the

       declare [-aAfFgiIlnrtux] [-p] [name[=value] ...]

So the statement above is an attempt to declare the name "a" as a
function, with the value somehow being "x".

There's a difficulty because recent Bashes have allowed function names
that are not "names" in the Bash sense:

       fname () compound-command [redirection]
       function fname [()] compound-command [redirection]
              This  defines  a function named fname.  [...]
              When  in  posix  mode,
              fname must be a valid shell name and may not be the name of one
              of the POSIX special builtins.  In  default  mode,  a  function
              name  can  be  any unquoted shell word that does not contain $.

       name   A  word  consisting  only of alphanumeric characters and under‐
              scores, and beginning with an alphabetic character or an under‐
              score.  Also referred to as an identifier.

In default mode, you actually can do
    $ function a=b { printf hi\\n; }
though you can't execute it:
    $ a=b foo
    bash: foo: command not found

You say the error is not very useful, but it seems to me that the error
is doing exactly what is intended; you *shouldn't* have an argument that
looks like an assignment.  IMO the fact that you can use "function" to
declare a function with "=" in its name is a mis-feature.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]