Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop shell wildcard character expansion?

Tags:

bash

wildcard

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.

like image 509
hotpaw2 Avatar asked Jul 12 '12 16:07

hotpaw2


People also ask

How do I stop a shell script?

If you are executing a Bash script in your terminal and need to stop it before it exits on its own, you can use the Ctrl + C combination on your keyboard.

What does ${} mean in shell script?

$() – the command substitution. ${} – the parameter substitution/variable expansion.

What is wildcard expansion?

Wildcard expansion can find all file names that end a certain way, but it can also find all file names that start a certain way.

What does $() mean in bash?

Example of command substitution using $() in Linux: Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).


2 Answers

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 
like image 190
c00kiemon5ter Avatar answered Sep 21 '22 15:09

c00kiemon5ter


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:

  • Unix shell

  • Pass Command Line Arguments To a Bash Alias Command

  • Csh - The C Shell

  • Bash Builtin Commands

  • Comparison with the Bourne shell and csh startup sequences

  • Alias Loop in csh

  • How to pass command line arguments to a shell alias?

  • Invoking program when a bash function has the same name

  • Special shell variables

  • C Shell Aliases with Command-Line Arguments

  • Preventing Wildcard Expansion / Globbing in Shell Scripts

like image 31
David Anderson Avatar answered Sep 20 '22 15:09

David Anderson