How reference a filename or path with spaces in a

2019-08-07 03:45发布

问题:

This question already has an answer here:

  • Bash variables with spaces 4 answers
  • How to cd into a directory with space in the name? 11 answers
  • How to add path with space in Bash variable 2 answers

I'm trying to write a simple shell script, but am struggling to figure out why I can't keep spaces in a string variable that I use to produce file names in a shell script.

#!/bin/bash

let minus3=10#$(date +%Y)-3
let minus2=10#$(date +%Y)-2
string="$minus3 - $minus2"
dir="'$minus3\ -\ $minus2'"
printf "\nIt is $(date +%Y).\nWe will be moving files from $string today.\n\n"

The ONLY CORRECTLY working command:

mkdir $minus3\ -\ $minus2

The commands that produce multiple incorrectly names directories because of the space:

mkdir $dir
mkdir $string```