简体   繁体   中英

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 \"$@\"'

How about this for zsh:

% alias f='noglob find . -iname $@'
% f podfi*
./Podfile
% f "podfi*"
./Podfile
%
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM