6

Hi I have a file with shell script which looks like this

#!/bin/bash 
todayDate=$(date)
echo "Today's date is  ${todayDate}. have a nice weekend!"

but when I call this file in my ubuntu terminal, I get such result

. have a nice weekend!eb 12 22:29:00 CET 2022

Why doesn't it show

Today's date is Sat Feb 12 22:31:55 CET 2022. have a nice weekend!

?? The image below is evidence that ubuntu shows weird echo in terminal

enter image description here

Someone asked me to upload result of ls -alh. This is the result

total 0
drwx------ 1 username username 512 Feb 12 22:07
drwx------ 1 username username 512 Feb 12 21:57
-rwx------ 1 username username 77  Feb 12 22:41 4Bbash

image version: enter image description here

1 Answers1

12

Your file has Windows Line endings \r\n.

  • \r means "Carriage return" (go back to beginnging of line)
  • \n means "Line feed" (go to next line).

Unix uses only \n as line ending.

Thus, your variable todayDate will include a \r at the end. This will place the cursor back to first position, and the text after it overwrites the previous text.

Set your text editor to use Unix Line Endings (Line Feed) or use dos2unix to fix your file. Check this or this.

pLumo
  • 26,947