0

I have a bash script thats cat'ing and then using awk to get some variables - the first one works fine but the second has a leading $ and a trailing \r ... thoughts?

#!/bin/bash
newTS=$( cat times.txt | awk -F';' '{print $2}')
ctime=$( cat times.txt | awk -F';' '{print $3}')
 echo 'nextTime='$newTS
 echo 'currentTime='$ctime

why does the echo command result in

+ echo $'currentTime=2020-04-21T12:36:14\r'

where as the first awk does not??

+ echo nextTime=2020-04-21T11:46:11

must be something with quoting or variable expansion ??

here's my times.txt file:

2020-04-21T11:45:11;2020-04-21T11:46:11;2020-04-21T12:36:14
  • Your input file is a DOS text file. I'm also assuming that you're running your script with bash -x, right? – Kusalananda Apr 21 '20 at 17:13
  • ah SOB DOS formatted! and yes bash -x – Tony Apr 21 '20 at 17:18
0

Your input file is a DOS text file. This means that the last character on each line that a Unix tool would see, is a carriage return character (commonly written \r).

You get this \r from the output of the second awk program because it's part of the third field of the line. It's not in the second field because the second field is not at the end of the line, so the first awk program does not generate a string with \r in it.

You additionally use bash -x to run your script. This enables tracing (debugging) output from the shell. When the shell outputs a trace of a command that includes a special character, such as the carriage return, it helpfully (?) quotes the associated string as a "C string", i.e. as $'...'.

Convert your input data file to a Unix text file using e.g. dos2unix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.