I have a line in a bash script that searches for a folder using find, and stores its path.
The line is as follows:
findfolder=$(eval echo $(eval "find "$HOME" -iname "$find_regex" -type d -print 2>/dev/null | sort -d | head -1" ))
and it works in almost any case, but, for some reason, if I search for a folder which has two spaces in it, it will return a path where all double spaces have been switched out for single spaces.
So, if I'm searching for a folder called "My music" with two spaces between "My" and "music", it will return "My music" with one space.
If I run the exact same command directly in terminal, I get the correct folder.
Why is this happening? (Let me know if more code is needed. Doesn't seem relevant since I ran the exact same command from terminal though. Don't want wall-of-text for no apparent reason now do we).
edit:
It is now working. The first eval or echo (not sure, but my tests suggests it was eval) was causing the output to collapse, removing "unnecessary" spacing. The working code is:
findfolder="$(eval "find "$HOME" -iname "$find_regex" -type d -print 2>/dev/null | sort -d | head -1" )"
evalisn't needed either, and will cause parsing problems in some cases (like if $find_regex matches several files in the current directory, or either it or $HOME contains a space, etc). BTW, $find_regex is not a regular expression, it's a pattern, which is quite different. For example, "xy?" as a regex matches 0 or more "x"es followed by an optional "y". "xy?" as a pattern matches a single "x" followed by any sequence of characters, followed by a "y", followed by any single character.