0

For some reason, resolving a variable name within backticks and double quotes causes an empty new line.

search="randomsite";
res="`find /var/www -maxdepth 2 -mindepth 2 -type d -name '${search}'`";
echo $res

1 Answer 1

2

Use $() rather than backticks - they are easier to read and nestable...

res=$(find /var/www -maxdepth 2 -mindepth 2 -type d -name "${search}")
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer. I was looking to know why my example didn't work though.
Yours doesn't work because you have put ${search} inside single quotes thereby preventing the shell from expanding the variable $search.
Try running find -name "${search}" at your shell prompt and that will work, whereas find -name '${search}' will not.
If this has solved your problem and answered your question, please accept my answer so I get a lovely big green tick. Else, please say what is still not correct, and I, or someone else, will try and assist you further. Thank you.

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.