3

I am trying to create the folder using:

mkdir $(date +%Y-%m-%d %H%M) 

So you see a space between the two and I know this is not best practice but I would like to get it to work.

I have tried to escape the space, use quotes but nothing seems to work and it's driving me mad.

Any ideas please?

dessert
  • 39,982

1 Answers1

7

You need quotes both around the whole $(...) command substitution (must be "double quotes") as well as around the format string argument to date ("double" or 'single' is okay):

mkdir "$(date '+%Y-%m-%d %H%M')"

If you don't quote arguments containing spaces (including command substitutions which could potentially have spaces in their result), they will be subject to word splitting by the shell, and you end up with multiple separate arguments instead of one, which will be interpreted differently by the command.

The main difference between "double" and 'single' quotes is that you can still have variable expansion and command substitution inside double quotes, whereas single quotes prevent any kind of evaluation and make the shell treat the string literally as it is.

Byte Commander
  • 107,489
  • Hello there friend, thank you for replying. When I try this I get an error that says cannot create directory ' ' : No such file or directory – always_learning Aug 10 '19 at 10:54
  • apologies I re read it and it gives me a better result but with ' ' either side of the folder name for example '2019-08-10 1058' Is there a way to loose the commas? – always_learning Aug 10 '19 at 10:59
  • There won't be any quotes around the resulting file name if you paste it like I wrote it in my answer. Maybe the way you are listing your directory contents is surrounding it with them because of the space? Check e.g. with ls -l – Byte Commander Aug 10 '19 at 11:03
  • Ahhh, thank you - it was the way the view was setup which I changed to list and there is no quotes! Wow, thank you soooo much - I have struggled with this for 1 week and finally decided to ask for help. Much appreciated mate :) – always_learning Aug 10 '19 at 11:06
  • Nice that I could help you. Now if this completely solved your question, please consider accepting the answer by clicking the grey check button on its left to mark your question as solved. Thanks. – Byte Commander Aug 10 '19 at 11:13
  • Sure, and again thank you - have a great weekend! – always_learning Aug 10 '19 at 11:16