Score:6

my ubuntu shows shell script echo in strange way

cl flag

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

pLumo avatar
in flag
might be your file has Windows Line endings?
game lover avatar
cl flag
@pLumo actually I don't know what is Windows Line endings .. I just coded and trying to run file
Merajul Arefin Pial avatar
br flag
How did you run this? Can you append command output of ```ls -alh``` in that folder which contains this script?
game lover avatar
cl flag
@MerajulArefinPial I posted my ls -alh result.
Merajul Arefin Pial avatar
br flag
did @pLumo 's solution worked?
game lover avatar
cl flag
@MerajulArefinPial I am trying to find way how to change windows line endings to unix line endings in notepad..
pLumo avatar
in flag
I don't think Notepad can do that, use a proper text editor, not such crappy software e.g. Notepad++ or VSCode or so.
game lover avatar
cl flag
@pLumo that's sad. Ok then I will try to download notepad++
pLumo avatar
in flag
Ask Microsoft why their software is still the same crap as 30 years ago.
pLumo avatar
in flag
See here for notepad++ https://askubuntu.com/a/645030/631600
Score:12
in flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.