Using Variable With Space In It?

Hey , I'm new to Bash and am having trouble with writing a script.

I have a variable and want to use it to create a tar file, but, I am having problems. Can anyone tell me what I'm doing wrong?

filestr=`date "+%y-%m-%d %H.%M.%S"`.tar
tar -cvf $filestr test.txt

So, basically, I'm creating a tar file based on the datetime in a specific format, but, the problem is that there is a space in the file name, so, the script fails during the tar creation.

Any ideas?

Enclose the tar file name you derived with double quotes, should work

$ tar -cvf "$(date "+%y-%m-%d %H.%M.%S").tar" work/sni.py

so in your example , it will be

$ filestr=`date "+%y-%m-%d %H.%M.%S"`.tar
$ tar -cvf "$filestr" test.txt

09-01-21 04.23.34.tar will be created. 

Quote "$filestr".

The best idea is Don't create files with spaces in their names.

The next important idea is to use the full year, not two digits:

filestr=`date "+%Y-%m-%d_%H.%M.%S"`.tar
tar cvf "$filestr" test.txt