How to use find command with variables
The first double-quoted one should work:
$ touch asdfghjkl
$ var=fgh
$ find -name "*$var*"
./asdfghjkl
Within single quotes ('*$var*'
), the variable is not expanded, and neither is it expanded when the dollar sign is escaped in double quotes ("*\$var*"
). If you double-escape the dollar sign ("*\\$var*"
), the variable is expanded but find
gets a literal backslash, too. (But find
seems to take the backslash as an escape again, so it doesn't change the meaning.)
So, confusing though as it is, this also works:
$ set -x
$ find -name "*\\$var*"
+ find -name '*\fgh*'
./asdfghjkl
You can try to run all the others with set -x
enabled to see what arguments find
actually gets.
As usual, wrap the variable name in braces {}
, if it's to be followed by letters, digits or underscores, e.g. "*${prefix}somename*"
.
Related videos on Youtube
yael
Updated on September 18, 2022Comments
-
yael almost 2 years
I am searching for files by
find
ing a partial file name:find /script -name '*file_topicv*' /script/VER_file_topicv_32.2.212.1
It works, but not when the partial file name is a variable:
var=file_topicv
find
reported file not found, (in spite of the file existing):find /script -name '*$var*'
What is wrong here?
I also tried these:
find /script -name "*$var*" find /script -name "*\$var*" find /script -name "*\\$var*"
but not one of those works.
Update:
I think this is the problem:
var=` find /tmp -name '*.xml' -exec sed -n 's/<Name>\([^<]*\)<\/Name>/\1/p' {} + | xargs ` echo $var generateFMN ls /script/VERSIONS | grep "$var" NO OUTPUT var=generateFMN ls /script/VERSIONS | grep "$var" VER_generateFMN_32.2.212.1
So why
$var
fromfind
command cause the problem? (I removed the spaces byxargs
.)-
Jeff Schaller over 6 yearsThe first double-quoted one didn’t work?
-
yael over 6 yearsno its not works
-
ilkkachu over 6 yearsSingle/double quotes difference: What's a good mnemonic for shell double vs. single quotes?, BashGuide Quotes
-
yael over 6 yearsok , but why my options are not working ?
-
Alessio over 6 yearswhat version of bash are you running?
bash --version | head -1
-
yael over 6 yearsGNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
-
Stéphane Chazelas over 6 yearsinstead of
echo $var
, tryprintf '%s\n' "$var" | sed -n l
so you can really see what it contains. -
ilkkachu over 6 years@yael, do something like
echo "$var" | od -c
, orprintf "%q\n" "$var"
to see what actually goes tovar
. -
ilkkachu over 6 years@yael, also please don't change your question after presenting, but give the context up front, it makes it much easier to answer the question you need an answer for...
-
yael over 6 yearsI get this --> printf "%q\n" "$var" then I get --> $'generateFMN\r'
-
yael over 6 yearswhy $'generateFMN\r' ?? instead to get generateFMN
-
-
Alessio over 6 yearsthe first double-quoted one also works for me. btw, worth mentioning that it will sometimes be necessary to use
${var}
rather than just$var
inside the double-quotes, depending on what comes after the variable....a*
is fine, a letter/digit/underscore/etc is not. -
yael over 6 yearssee my update , this is the problem , when $var comes from find & this isnt works
-
ilkkachu over 6 years@cas, they almost certainly have a CR in there, the sed here won't remove it or any other trailing garbage on the line. yael posted another question on it: unix.stackexchange.com/questions/417756/…