0

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" )"
5
  • 1
    Interesting. But do you need the double $($()) and the evals? Commented Mar 31, 2012 at 21:28
  • @PéterVarga Well I didn't get it to work before I used eval. They fixed the problem with assigning findings to a variable using echo. Commented Mar 31, 2012 at 21:30
  • 1
    @PéterVarga I now solved it. The first eval and echo is not needed, and was causing the collapse. Weird that I at some point found that they were necessary Commented Mar 31, 2012 at 21:42
  • The second eval isn'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. Commented Apr 1, 2012 at 0:17
  • @GordonDavisson yup true about pattern vs regex. rename should fix that. But you're wrong about the problem with finding several files. and neither $home nor $findregex contains spaces (or ever will in my case). I don't think you're wrong about it being unnecessary though :) Commented Apr 1, 2012 at 10:05

1 Answer 1

3

I do not understand what you are trying to do with the eval statements. However, you may experiment with quoting the command substitution. Please note the difference between the following two command lines:

# the two spaces between foo and bar are collapsed
echo $( echo "foo  bar" )

# the two spaces between foo bar bar are kept
echo "$( echo "foo  bar" )"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.