120

Is there any way for a compiled command-line program to tell bash or csh that it does not want any wildcard characters in its parameters expanded?

For instance, one might want a shell command like:

foo *

to simply return the numeric ASCII value of that character.

4 Answers 4

176

No. The expansion takes place before the command is actually run.
You can only disable the glob before running the command or by quoting the star.

$ # quote it
$ foo '*'

$ # or escape it
$ foo \*

$ # or disable the glob (noglob)
$ set -f
$ foo *

$ # alternative to set -f
$ set -o noglob
$ # undo it by 
$ set +o noglob
8
  • 4
    also, if there is nothing to expand, there is no expansion. i.e. with an empty pwd, echo * is just * Mar 31, 2014 at 23:48
  • 9
    @sam This actually depends on how your options are set. From tldp: "If no matching file names are found, and the shell option nullglob is disabled, the word is left unchanged. If the nullglob option is set, and no matches are found, the word is removed." Feb 6, 2015 at 18:05
  • thanks, I should've qualified that that was just from observation not from any understanding. Feb 6, 2015 at 21:31
  • @c00kiemon5ter How to undo the set -f ? Is it unset -f?
    – Nam G VU
    May 17, 2019 at 8:57
  • 3
    @NamGVU set +f Feb 26, 2020 at 7:32
73

While it is true a command itself can not turn off globbing, it is possible for a user to tell a Unix shell not to glob a particular command. This is usually accomplished by editing a shell's configuration files. Assuming the command foo can be found along the command path, the following would need to be added to the appropriate configuration file:

For the sh, bash and ksh shells:

alias foo='set -f;foo';foo(){ command foo "$@";set +f;}

For the csh and tcsh shells:

alias foo 'set noglob;\foo \!*;unset noglob'

For the zsh shell:

alias foo='noglob foo'

The command path does not have to be used. Say the command foo is stored in the directory ~/bin, then the above would become:

For the sh, bash and ksh shells:

alias foo='set -f;foo';foo(){ ~/bin/foo "$@";set +f;}

For the csh and tcsh shells:

alias foo 'set noglob;$home/bin/foo \!*;unset noglob'

For the zsh shell:

alias foo='noglob ~/bin/foo'

All of the above was tested using Apple's OSX 10.9.2. Note: When copying the above code, be careful about deleting any spaces. They may be significant.

Update:

User geira has pointed out that in the case of a bash shell

alias foo='set -f;foo';foo(){ ~/bin/foo "$@";set +f;}

could be replaced with

reset_expansion(){ CMD="$1";shift;$CMD "$@";set +f;}
alias foo='set -f;reset_expansion ~/bin/foo'

which eliminates the need for the function foo.

Some web sites used to create this document:

5
  • 5
    This should have been the accepted answer as it proves it is indeed possible to do what the OP wants. For some even better methods in bash, this link documents the various options: blog.edwards-research.com/2011/05/preventing-globbing
    – geira
    Apr 23, 2015 at 8:40
  • @geira: Sometimes an answer is accepted before a better answer is posted. I believe this may be one of those cases. I also believe the answer marks as most usefull by users ends up first. Note: I added your input to my answer. Apr 27, 2015 at 14:11
  • @geira, the OP wants the compiled program to communicate to the shell; answering that it's possible to configure the shell to behave as desired on a command-by-command basis is a useful answer, and I myself would accept this one, but I do see room to disagree as to whether it's entirely on-point. Dec 1, 2015 at 0:24
  • The fact that it can be done does not mean that it should be done. Having the shell's behavior change from the user's reasonable expectation for select commands sounds like surprising and ultimately undesirable.
    – tripleee
    Jul 29, 2016 at 5:36
  • @tripleee: There was a time when people expected relays and vacuum tubes. Expected were holes punched in cardboard cards and paper tape. People held slide rules instead calculators and phones. I suppose for you this would be desirable. I did not care much for those times. I do enjoy seeing what is next. Jul 29, 2016 at 11:31
10

The expansion is performed by the shell before your program is run. Your program has no clue as to whether expansion has occurred or not.

   set -o noglob

will switch off expansion in the invoking shell, but you'd have to do that before you invoke your program.

The alternative is to quote your arguments e.g.

foo "*"
2
  • 4
    setopt appears to be zsh-specific command. As another poster said, for bash you use set -o noglob to disable wildcard expansion. Mar 4, 2016 at 21:31
  • Now amended. Thank you Jun 1, 2018 at 11:45
2

Beware: if there are no names matching the mask, bash passes the argument as-is, without expansion!

Proof (pa.py is a very simple script, which just prints its arguments):

 $ ls
f1.cc  f2.cc  pa.py
 $ ./pa.py *.cc
['./pa.py', 'f1.cc', 'f2.cc']
 $ ./pa.py *.cpp
['./pa.py', '*.cpp']
1
  • 5
    Not always. set -o nullglob or shopt -s failglob, and this behavior will change (in two very different ways). Dec 1, 2015 at 0:22

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.