help-bash
[Top][All Lists]
Advanced

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

type -aP: intentional behaviour or a bug?


From: Adam Vodopjan
Subject: type -aP: intentional behaviour or a bug?
Date: Fri, 16 Dec 2022 02:18:14 +0200

Hey guys!

I need to expand a command to all its locations under $PATH. For example,
on my system /bin is a symlink to /usr/bin so for 'ls' there should be
both /usr/bin/ls and /bin/ls listed.

For the purpose I wanted to use either 'type -ap' or 'type -aP'. I know
there is a difference in 'type -p' vs 'type -P':

$ alias ls='ls -1'
$ type -p ls
$ type -P ls
/usr/bin/ls

But I was sure '-ap' should be the same as '-aP'. Somehow I've found out
this:


$ hash -t ls                # check if ls has not been hashed yet
bash: hash: ls: not found   # not hashed
$ type -ap ls
/usr/bin/ls
/bin/ls
$ type -aP ls
/usr/bin/ls
/bin/ls
$ ls >/dev/null             # hash ls
$ hash -t ls                # and check it
/usr/bin/ls                 # hashed now
$ type -ap ls
/usr/bin/ls
/bin/ls
$ type -aP ls
/usr/bin/ls                 # why a single result only ??


I tracked it down to this chunk in builtins/type.def:


  /* If the user isn't doing "-a", then we might care about
     whether the file is present in our hash table. */
  if (all == 0 || (dflags & CDESC_FORCE_PATH))
    {
      if (full_path = phash_search (command))
        {
          if (dflags & CDESC_TYPE)
            puts ("file");
          else if (dflags & CDESC_SHORTDESC)
            printf (_("%s is hashed (%s)\n"), command, full_path);
          else if (dflags & (CDESC_REUSABLE|CDESC_PATH_ONLY))
            printf ("%s\n", full_path);

          free (full_path);
          return (1);
        }
    }


So with CDESC_FORCE_PATH bit set (-P flag) it completely ignores -a flag.
Was it intentional or is it a bug? I don't get the point about such code
especially assuming this line:


  case 'P':        /* shorthand for type -ap */


How come -P is a shorthard for -ap ??

I'm totally confused :/



reply via email to

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