Skip to main content
added 96 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

(to list the contents of the non-hidden directoriesfiles whose name ends in .txt, or except for those of type directory where it lists their contents instead; maybe you meant to use the non-directory-d option to avoid that special treatment of directory files with the same name patterns).

With nullglob on would run ls with no argument which is treated as ls -- . (list the contents of the current directory) if no files match, which is probably worse than calling ls with a literal *.txt as argument².

files=( *.txt(N))  ) # zsh
files=( ~(N)*.txt ) # ksh93

(to list the contents of the non-hidden directories whose name ends in .txt, or the non-directory files with the same name patterns)

With nullglob on would run ls with no argument which is treated as ls -- . (list the current directory) if no files match, which is probably worse than calling ls with a literal *.txt as argument².

files=(*.txt(N))  # zsh
files=(~(N)*.txt) # ksh93

(to list files whose name ends in .txt except for those of type directory where it lists their contents instead; maybe you meant to use the -d option to avoid that special treatment of directory files).

With nullglob on would run ls with no argument which is treated as ls -- . (list the contents of the current directory) if no files match, which is probably worse than calling ls with a literal *.txt as argument².

files=( *.txt(N)  ) # zsh
files=( ~(N)*.txt ) # ksh93
added 140 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

The nullglob option (which BTW is a zsh invention, only added years later to bash (2.0))option¹ would not be ideal in a number of cases. And ls is a good example:

With nullglob on would run ls with no argument which is treated as ls -- . (list the current directory) if no files match, which is probably worse than calling ls with a literal *.txt as argument¹argument².

bash (since version 3) has a failglob option for that (interesting to this discussion, since contrary to ash, AT&T ksh or zsh, bash doesn't support local scopes for options²options³, that option when enabled globally does break a few things like the bash-completion functions).

¹ added in 2.0 in 1996 with the introduction of the shopt builtin, named after zsh's equivalent option, though bash had the allow_null_glob_expansion variable for that in earlier versions

² for which ls would likely report an error that the *.txt file doesn't exist, unless it has been created in the interval, or the current directory happens to be searchable but not readable and that file or directory exists. Try after mkdir -p '*.txt/wtf'; chmod a=,u=wx . for instance

²³ version 4.4 saw some improvement on that front in that options set by set -o could be made local to functions with local - like in the Almquist shell, but that doesn't work for bash's second set of options, the ones set with shopt

The nullglob option (which BTW is a zsh invention, only added years later to bash (2.0)) would not be ideal in a number of cases. And ls is a good example:

With nullglob on would run ls with no argument which is treated as ls -- . (list the current directory) if no files match, which is probably worse than calling ls with a literal *.txt as argument¹.

bash (since version 3) has a failglob option for that (interesting to this discussion, since contrary to ash, AT&T ksh or zsh, bash doesn't support local scopes for options², that option when enabled globally does break a few things like the bash-completion functions).

¹ for which ls would likely report an error that the *.txt file doesn't exist, unless it has been created in the interval, or the current directory happens to be searchable but not readable and that file or directory exists. Try after mkdir -p '*.txt/wtf'; chmod a=,u=wx . for instance

² version 4.4 saw some improvement on that front in that options set by set -o could be made local to functions with local - like in the Almquist shell, but that doesn't work for bash's second set of options, the ones set with shopt

The nullglob option¹ would not be ideal in a number of cases. And ls is a good example:

With nullglob on would run ls with no argument which is treated as ls -- . (list the current directory) if no files match, which is probably worse than calling ls with a literal *.txt as argument².

bash (since version 3) has a failglob option for that (interesting to this discussion, since contrary to ash, AT&T ksh or zsh, bash doesn't support local scopes for options³, that option when enabled globally does break a few things like the bash-completion functions).

¹ added in 2.0 in 1996 with the introduction of the shopt builtin, named after zsh's equivalent option, though bash had the allow_null_glob_expansion variable for that in earlier versions

² for which ls would likely report an error that the *.txt file doesn't exist, unless it has been created in the interval, or the current directory happens to be searchable but not readable and that file or directory exists. Try after mkdir -p '*.txt/wtf'; chmod a=,u=wx . for instance

³ version 4.4 saw some improvement on that front in that options set by set -o could be made local to functions with local - like in the Almquist shell, but that doesn't work for bash's second set of options, the ones set with shopt

might as well use the standard set -o syntax
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

You can get that behaviour with zsh with setoptset -o cshnullglob though a more sensible way to do it in zsh would be to use a glob like:

You can get that behaviour with zsh with setopt cshnullglob though a more sensible way to do it in zsh would be to use a glob like:

You can get that behaviour with zsh with set -o cshnullglob though a more sensible way to do it in zsh would be to use a glob like:

added 443 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading
added 230 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading
added 50 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading
added 12 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading
added 1 character in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading
added 21 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading
added 21 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading
edited body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading
added 55 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading
added 55 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading
added 264 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading
added 159 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading
added 1364 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading
deleted 3 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading