How do I use variables in a filename correctly? This is what I'm doing, but it seems the underline makes some problems:


if [ -f /opt/nginx/conf.d/com_$domain_$sub.conf ]
  cat <<EOF > /opt/nginx/conf.d/com_$domain_$sub.conf
some multiline
  • Have you tried quoting it? Always double quote your variables. – Jesse_b Oct 4 '17 at 21:41
  • I've quoted the complete path... – user3142695 Oct 4 '17 at 21:41
  • Related: $VAR vs ${VAR} and to quote or not to quote – steeldriver Oct 4 '17 at 21:51
  • 3
    If you stop deleting your questions as soon as someone comments with the correct answer, you might just have someone help you with sorting this program out. You've done that at least twice today. That syntax error question: there were other thing wrong in the code that could have been fixed and made more efficient, but that's impossible get to now, because you deleted the question as soon as someone told you what the syntax error was due to. – Kusalananda Oct 4 '17 at 21:56

In this case Bash doesn't know where the variable name ends. Try like this:

  • That's working. But I do get a permission denied error for the cat command. Do I have to change chmod of the conf.d directory? – user3142695 Oct 4 '17 at 21:49
  • @user3142695 Probably; or run the script as different user. I don't know what you are trying to do exactly; nor what the permissions are etc. Before you give this information here please note this is not the right place. One problem, one question. "Permission denied" is a different problem. You may ask another question (but do your own research first). – Kamil Maciorowski Oct 4 '17 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.