1

I have observed that when I create a directory using a variable myvar=2099-100 mkdir $myvar && echo "done", bash adds a whitespace at the end of the directory name. The issue I have here is that this behavior seems to be very inconsistent.

If I run mkdir $myvar&& echo "done" the whitespace disappears. If I run mkdir ${myvar} && echo "done" the whitespace disappears.

The inconsistency lies in the fact that if I create the directory without whitespace at the end once, then I can't reproduce the issue. Running the same command that would create a directory with a whitespace does create it without it at the end.

So there are two questions: What is the best practice when creating directories using bash. Should variable assignment contain quotes? What is the correct way of referencing the variable when running mkdir (is it ${var} notation?)? Why is bash adding whitespace when running mkdir $var && but not adding them when mkdir $var (I guess it has to do with expansion)? Why after creating directory without a whitespace I can't reproduce creation with a whitespace? Is the expanded variable cached somewhere?

5
  • 2
    Not experiencing what you are claiming here. Can you show an actual paste of how it manifest? (Maybe with -v for mkdir? ls?) Also have you done anything to IFS? Commented Jun 15, 2021 at 9:55
  • 1
    The best practice is to double-quote variables. Usually lack of quoting loses whitespace (if any) rather than adds it, I have no idea what happened in your case. Nevertheless you definitely should double-quote in Bash. Commented Jun 15, 2021 at 9:59
  • 1
    Another useful link: When is double-quoting necessary? Personally I tend to quote even when it's not necessary. It's easier for me to remember one rule that says "always quote" than to analyze if I can get away without quotes. Commented Jun 15, 2021 at 10:05
  • 1
    If this is in a script file, check the line endings (cat -A file and look for "^M" as the end of lines) and use dos2unix as required. Commented Jun 15, 2021 at 14:36
  • 1
    Are you accidentally typing something like a non-breaking space (which looks like a normal space, but doesn't act like one) between $myvar and && (maybe by holding down shift or something like that)? Try viewing the filename with ls | LC_ALL=C cat -vet and see what it looks like; normally, that'll just add a "$" at the end of each line, but if there are other weird characters it'll give an alternate representation of them. Commented Jun 15, 2021 at 23:35

1 Answer 1

0

The comments where right, the non breaking space was the issue. I may have forgotten to add that I'm running script on a remote server where I'm using heredoc to paste the script (unfortunately this is the workaround, terminal hijacking does not work). I'm copying the text from windows terminal (WSL) and we all know how windows clipboard works these days.

As for why I couldn't reproduce the issue, once the file was modified in vim the correct space character was introduced, I've removed the space copied from the text file.

What I've learned here is to sanitize my input before pasting it to the terminal as that may cause a lot of not so obvious issues.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.