Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: (N) defaults to whole directory listing

On Fri, Jan 29, 2021 at 4:24 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
> Is this really what we want?
>     $ ls -1 no_such_file
>     ls: cannot access 'no_such_file': No such file or directory
>     $ ls -1 no_such_file 2> /dev/null
>     $ ls -1 no_such_file(N)
>     aaa
>     aa-messagefunctions
>     aliases
>     aliases,1,remove _find_match
>     aliases,2,STARTCOM
>     ...
>     ....  lists the whole directory.  It's not exactly what one might
>     expect. I can see that if the argument 'disappears' entirely then
>     'ls' thinks 'ls *' but shouldn't 'ls' be in a 'not found' condition
>     rather than a 'list all' condition?

`ls` without arguments lists files in the current directory. This is
mandated by POSIX and what everyone expects.

`no_such_file(N)` expands to nothing if there is no file named
`no_such_file` in the current directory. This is in line with the zsh
documentation and what everyone expects.

If your examples aren't artificial but really show what you are trying
to do, you can do it like this:

  print -rC1 -- no_such_file(N)

This will print `no_such_file` if such file exists and will do nothing

If you really need that `ls` call (perhaps because you are passing
some flags to it), you can do this:

  ( ls -1 no_such_file(#q) ) 2>/dev/null

This does the same as above but about 1000 times slower and returns an
error if the file doesn't exist.

You can also use an explicit condition:

  if [[ -e no_such_file ]]; then
    ls -1 no_such_file


Messages sorted by: Reverse Date, Date, Thread, Author