0

I was working on a script to echo the date upon login in a different format but once I get down to echoing the result, it gives me a jumbled up output. I've been searching online to see if I'm calling the variables wrong or using wrong ticks somewhere but no luck. I even have echoed each individual variable before and after the problem echo and they echo the proper date/month/day of week. As my script is right now, it only puts out ". which is a Thu" when run. Also, I've been executing it with "sh ./datescript.sh" Any help/additional resources would be appreciated. Thanks!

My Script:

#!/usr/bin/env bash
date=`date`     #NOTE: date being used in two different ways
day=`echo ${date} | cut -f1 -d' '`
month=`echo ${date} | cut -f2 -d' '`
date=`echo ${date} | cut -f3 -d' '`
echo "Today is the ${date}th day of ${month}, which is a ${day}."
echo $day
echo $month
echo $date
5
  • 2
    Your script works correctly on my systems. Commented Dec 4, 2014 at 16:18
  • Alright... I'm going to try it through vi instead and see if it works. I was writing it in Notepad++ and copying over via ftp. Commented Dec 4, 2014 at 16:25
  • 1
    @Xattle - you may want to run dos2unix on it before attempting to run it Commented Dec 4, 2014 at 16:26
  • @Xattle that is indeed your problem. Notepad++ by default will use Windows new line characters CRLF, and bash won't interpret those properly as it's looking for UNIX new line characters LF only. In notepad++ you can click Edit --> EOL Conversion --> UNIX/OSX Format. Commented Dec 4, 2014 at 16:30
  • Why not just date +"Today is the %-dth day of %B, which is a %A."? This is unnecessarily complicated... Commented Dec 4, 2014 at 19:28

2 Answers 2

2

I can replicate the behaviour if I add $'\r' at the end of month and date assignment lines. Seems like Win/*nix line ending issue.

Run dos2unix or fromdos on the script to fix it.

Sign up to request clarification or add additional context in comments.

1 Comment

It was a Win/*nix issue. Once I tried it through vi, it worked fine. Switching to UTF-8 encoding also ended up working.
2
#!/usr/bin/env bash

day=`date +%A`
month=`date +%B`
date=`date +%-d`

echo "Today is the ${date}th day of ${month}, which is a ${day}."
echo $day
echo $month
echo $date

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.