From 84f175620774011e55634d24dd39834eb31d394a Mon Sep 17 00:00:00 2001 From: Tony Duckles Date: Fri, 8 Mar 2013 17:36:18 -0600 Subject: [PATCH] bin/ack-wrapper: Correctly escape user-supplied args When using "ack needle foo*.txt", the shell will expand that wildcard and if any of the matching files contain spaces (or other special chars that need escaping) those won't be passed correctly to 'ack' when using the old-style "command ack $ackrc $*" calling method. We need to single-quote wrap any arguments to ensure we don't need to escape-out any spaces/exclamation/question chars. --- bin/ack-wrapper | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/bin/ack-wrapper b/bin/ack-wrapper index 49e5386..7459a2c 100755 --- a/bin/ack-wrapper +++ b/bin/ack-wrapper @@ -5,7 +5,8 @@ PWD=$(pwd) HOME=~ -ACKARGS="" +ack=$(type -P ack) +ackrc="" match="" dir="$PWD" @@ -14,7 +15,7 @@ while [ "$dir" != "/" -a "$match" = "" ]; do if [ "$dir" != "$HOME" ]; then match=1 echo "(Include: ${dir}/.ackrc)" - ACKARGS="$(egrep "^[^#]" "${dir}/.ackrc" | xargs) $ACKARGS" + ackrc=$(egrep "^[^#]" "${dir}/.ackrc" | tr '\n' ' ') else match=0 fi @@ -23,4 +24,9 @@ while [ "$dir" != "/" -a "$match" = "" ]; do fi done -command ack $ACKARGS $* +# Add quote-wrapping for any additional args to ensure proper passing to +# real 'ack'. +for arg in "$@"; do ackrc="${ackrc} '${arg}'"; done + +# Run the final command +eval "${ack} ${ackrc}" -- 2.45.2