I want to use ack-grep alias in .zshrc. Here is how my alias lookes like now:
alias 'ack'='ack-grep -C -i -G --match'
but what I need is to put strings from this:
% ack string1 string2
inside
alias 'ack'='ack-grep -C -i -G string1 --match string2'
How to make this?
Here is the code that worked:
ack() {
case $# in
1) args="$1";;
2) args="$1 -G $2";;
*) args="$@";;
esac
eval "ack-grep -iC $args"
}
In my case I need to use eval
for using more than one variable.
Updated code without security issues:
ack () {
if (( $# == 2 )); then
ack-grep -iC "$1" -G "$2"
elif (( $# == 1 )); then
ack-grep -iC "$1"
else
echo 'Sorry, function works only for one and two params. In other case use ack-grep. Reread .zshrc'
fi
}
eval
is rarely necessary.
ack () {
if (( $# > 2 )); then
ack-grep -iC "$@"
else
ack-grep -iC "$1" ${2:+-G "$2"}
fi
}
Update: here is an exact version of your first solution, without eval
.
ack() {
case $# in
1) ack-grep -iC "$1";;
2) ack-grep -iC "$1" -G "$2";;
*) ack-grep -iC "$@";;
esac
}