Sébastien
Sébastien

Reputation: 14821

Escape quotes in alias

I want to write an alias to quickly search for a filename in the current directory (case insensitive).

The following works:

alias f='find . -iname $@'

The problem is that when I want to use wilcards in the search expression, I need to quote them (otherwise zsh expansion handles the wildcard first):

> f podfi*
zsh: no matches found: podfi*
> f "podfi*"
./Podfile

How can I add the quotes in my alias so that f podfi* works ?

These tentatives do not work:

alias f='find . -iname "$@"'
alias f='find . -iname \"$@\"'

Upvotes: 3

Views: 472

Answers (2)

sokin
sokin

Reputation: 844

How about this for zsh:

% alias f='noglob find . -iname $@'
% f podfi*
./Podfile
% f "podfi*"
./Podfile
%

Upvotes: 2

Renaud Pacalet
Renaud Pacalet

Reputation: 29025

function ff () { find . -iname "$2"; GLOBIGNORE="$1"; }
alias f='s="$GLOBIGNORE"; GLOBIGNORE="*"; ff "$s"'

The alias saves the current value of GLOBIGNORE, sets GLOBIGNORE to *, and calls the ff function to which it passes the saved GLOBIGNORE as a first parameter. The ff function calls find with the unglobled alias parameter and restores GLOBIGNORE. Tested with GNU bash, version 4.3.30(1)-release:

f *.tex
./full.tex
./hdr.tex
./main.tex

Upvotes: 2

Related Questions