I am trying to define the following bash function in my .bashrc
file:
function myfind() {
find $1 -not -path venv -not -path .tox -name "$2" | xargs grep -n "$3"
}
which is not doing what I expect. When I use that function to search for text in files, e.g.
myfind . *.py test
it does not return anything. But when I use the expression directly, i.e.
find . -not -path venv -not -path .tox -name "*.py" | xargs grep -n "test"
it returns some matches.
I already tried to use double quotes like
function myfind() {
find $1 -not -path venv -not -path .tox -name '"$2"' | xargs grep -n '"$3"'
}
or
function myfind() {
find $1 -not -path venv -not -path .tox -name "'$2'" | xargs grep -n "'$3'"
}
and tried to escape the quotes like
function myfind() {
find $1 -not -path venv -not -path .tox -name \"$2\" | xargs grep -n \"$3\"
}
but none of these seem to be working.
How to fix this bash function so the quotes are being "used" around the arguments?
myfind . "*.py" test
? – Philippos May 18 '22 at 11:17find
to keep the shell from expanding it. The same applies to your find-script. You can't solve that problem inside the script because the damage is already done before the script gets to see the parameter. – Philippos May 18 '22 at 12:35help function
for correct syntax. – Cyrus May 18 '22 at 21:50