Unix & Linux Asked by nonox on November 26, 2020
Cannot have space within string variable for -path or -iregex find’s option.
This input '/home/demo.*/.config/File System/.*t$'
is fed on:
read i
m="-iregex $i"
find ~ $m
won’t work as it will if constant, not variable
so is not find ~ '$m'
nor eval find ~ '$m'
How’s the correct solution ?
Don't put actions into a string variable that contains the data (see Bash FAQ 50). And always double-quote your variables when you use them
i='/home/demo.*/.config/File System/.*t$'
find ~ -iregex "$i"
If you want to include the -iregex
only if $i
is not empty then you can do something like this, which keeps it but replaces the regular expression with .*
(i.e. anything)
find ~ -iregex "${i-.*}"
If you are using a shell such as bash
(but not sh
) that understands arrays you can use one to include the -iregex
only if needed
IFS= read -r i
findRegex=()
[[ -n "$i" ]] && findRegex=('-iregex' "$i")
find ~ "${findRegex[@]}"
To see what's going on here you can modify find
to echo find
. Reading man bash
and searching for Arrays
and/or @
may also help.
Notice I've also changed your read i
to IFS= read -r i
. This stops the shell trying to parse your input data, which can break expressions containing multiple spaces (characters in $IFS
to be precise) and other character combinations
Correct answer by roaima on November 26, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP