$ bash --version
GNU bash, versione 5.2.26(1)-release (x86_64-pc-linux-gnu)
I don't know how to deal with $VAR when its value inside contains single quote ('
) and/or backtick (`
).
I'm in the need of executing find's -exec
based on entries found in list.txt
.
list.txt
contains:
- one FILENAME.EXT per line
- each line ends with newline (
\n
)
As a preventive test to verify everything is OK, I run these:
$ while IFS= read -r song; do find /mnt/EXTHDD/unsorted -type f -name "$song"; done < list.txt
$ while IFS= read -r song; do find /mnt/EXTHDD/unsorted -type f -name "*$song*"; done < list.txt
$ while IFS= read -r song; do find /mnt/EXTHDD/unsorted -type f -name '$song'; done < list.txt
$ while IFS= read -r song; do find /mnt/EXTHDD/unsorted -type f -name "${song}"; done < list.txt
None worked...
Unfortunately, not all occurrences are listed due to Special Characters in FILENAME and thus $song
variable cannot be quoted correctly (neither with double quotes, nor single quotes).
Example of lines FILENAME.EXT found in list.txt
:
Pino Daniele ~ [2000] Napul`e' - Raccolta completa ~ 01.14) `O ssaje comme fa `o core.mp3
[CONCERTI] Malora -- Live@TANT`E'! (Agosto '05, Villa Fossa` [presa diretta, mixer:stereo]) -- #15. 20:30 ... Tutto tace?.wave
[...]
, which is a shell glob, e.g.,[2000]
matches a2
or0
,[CONCERTI]
matchesC
,O
,N
,E
,R
,T
orI
."$song"
and"${song}"
(and"*$song*"
, if you want to pass asterisks) are properly quoted and the value of the variable cannot break them. Bash cannot store null bytes in a variable, but filenames (pathnames in general) also cannot contain null bytes, so this shouldn't be a problem in this usage case. AFAIK everything else can be stored in a variable. If your code does not work as expected, it's not because allegedly "the variable cannot be quoted correctly"; it can.find
doesn't have the equivalent ofgrep -F
. Not even in GNU find, even though it supports various regex dialects via the-regextype
option.-name
(or-iname
) behaviour infind
; you're right, indeed. Thanks, dear! unix.stackexchange.com/questions/773436/… yes! This helped me