I am trying to create a directory with a timestamp. If I type at the prompt

mkdir "$(date +%Y-%m-%d_%H%M%S)"

I get a directory named with the date as I would like. If I put the same line in a bash script I get some strange characters at the end of the name. What is going on? I am attempting this in Cygwin and tried bash and sh.

  • 4
    Can you post an actual script and the actual output you get?
    – Crowman
    Jul 17 '14 at 20:18
  • 1
    It's probably your $PATH and/or shell builtins/functions/aliases. Verify that command -V date and ENV=/dev/fd/0 sh <<< 'command -V date' produce the same output.
    – mikeserv
    Jul 17 '14 at 21:01
  • 1
    Or... And I should have noticed this before... You should definitely be quoting the +... bit like "$(date '+%Y-%m-%d_%H%M%S')" - though I'm not certain off the top of my head why that would behave differently in a script than at the prompt.
    – mikeserv
    Jul 17 '14 at 21:08
  • Check your LC_TIME
    – tijagi
    Jul 18 '14 at 0:37

I had same problem with Cygwin. You need to run

dos2unix mkdatedir.sh  # or whatever your script is called

this will remove any dos carriage returns

but better to set option igncr

see https://cygwin.com/ml/cygwin-announce/2010-08/msg00015.html

  • Ahh, very good. This makes perfect sense.
    – mikeserv
    Jul 19 '14 at 4:50

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.