3

I have a bash script that's just a shortcut to the grep command. I call it like this:

stx -p /var/www -t 'my text'

This would run this command:

sudo grep -rinI /var/www -e 'my text'

I'm trying to add in the ability to include or exclude files with a certain extension (.css, .js, etc.). Here is the script:

#!/usr/bin/bash
#search for text in files

set -x #echo all commands

while [ -n "$1" ]; do
        case "$1" in 
                -p) path="$2" # path to start search in
                shift
                ;;  
                -t) txt="$2" # text to search for
                shift
                ;;   
                -i) inc="$2"    
                        #echo 'inc: ' $inc
                        include="--include=\*.${inc}" # file type to include
                shift
                ;;  
                -?) 
                        echo 'stx usage:'
                        echo '-p    path to search in'
                        echo '-t    text to search for'
                        exit 0
                shift
                ;;
        esac 
        shift
done


result=$(sudo grep -rinI $include "$path" -e "$txt")
echo "$result"

#       grep options:
#       r               recursive
#       i               case-insensitive
#       n               show line number   
#       I               ignore binary files
#       --include       only include files with specified extension

When I run this, for some reason it puts single quotes around the $include variable, so if I try to run this to search .js files for "my text":

stx -p /var/www -t 'my text' -i js

it produces this:

sudo grep -rinI '--include=\*.js' /var/www -e 'my text'

The single quotes around the --include*.js are breaking it. How can I stop it from inserting those quotes?

raphael75
  • 723

1 Answers1

6

As muru pointed out this is caused by the backslash escape being used here:

-i) inc="$2"    
  #echo 'inc: ' $inc
  include="--include=\*.${inc}" # file type to include

The * wildcard does not expand inside double quotes which means it's already being escaped (by the quotes), this means that the backslash escape will be treated literally as well.

You could change it to:

-i) include="--include=*.${2}";;

Here you can see how the backslash escape is treated in various scenarios:

$ echo \*
*
$ echo "\*"
\*
$ echo "\$"
$

In the first example it is removed after it escapes *
In the second example it is not needed to escape the * so it is left literally
In the third example it is needed to escape the $ (which can expand inside single quotes) so it is removed.

jesse_b
  • 37,005