Tizianoreica
Tizianoreica

Reputation: 2236

Concat Variable (date) and String in shell unix - bash

I'm trying to concat a var (date) and string to have a file with current date name.

My code.

days="$(date +"%Y%m%d_%H%M")"
echo "SKIP" > ${days}_EMERGENCY.txt

but when I run, I get a file with a ? in file name, like this:

enter image description here

Am I doing something wrong?

EDIT

Looking at symbol, ? stands for \r - could it be because I'm writing on notepad and then upload via ftp the .sh script?

EDIT 2

Tried with vi on local machine - now it's also worse. enter image description here

Upvotes: 6

Views: 17987

Answers (3)

Stefan Hegny
Stefan Hegny

Reputation: 2187

I guess your vi will have made the entire file DOS-style and so there will be another carriage return at the end of the echo statement

Try dos2unix or using an editor that allows you to change the line-ending style or

sed -i "s/$( printf '\015' )//g" yourscript

Upvotes: 6

Dilettant
Dilettant

Reputation: 3345

It works on my system (OS X) even with double quotes everywhere. For variation I used:

$> days="$(date +'%Y%m%d_%H%M')"
$> echo $days
20160601_1051
$> echo "SKIP" > ${days}_EMERGENCY.txt
$> ll ${days}_EMERGENCY.txt
-rw-r--r--  1 user  team  5 Jun  1 10:52 20160601_1051_EMERGENCY.txt

Upvotes: 5

alijandro
alijandro

Reputation: 12167

try to remove the redundant double quote in your first variable assignment.

$ days=$(date +%Y%m%d_%H%M)
$ echo $days #see what output will get?
$ echo "SKIP" > ${days}_EMERGENCY.txt

Upvotes: 1

Related Questions