0

I am setting a variable as follows and when I print it out I get something unexpected.

sql="values ('$yy-$mm-$dd $time','$tz', $load)"
printf "%s\n" "$sql"
)alues ('2011-01-01 23:55:00','EST', 5081.2
)alues ('2011-01-01 23:55:00','EST', 475.8
)alues ('2011-01-01 23:55:00','EST', 1574.9

Somehow the closing parenthesis is at the beginning of the line?! I check $load to make sure there is no newline character in it.

I am not sure what to try.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
Chait
  • 1
  • 2
  • 1
    I suspect one of your variables ($load?) contains a line feed. – Cyrus Mar 22 '22 at 00:00
  • Your `load` variable is probably coming from a source that uses DOS/Windows line endings (which have a carriage return in addition to the linefeed that unix programs expect. See ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) Note that both carriage return and linefeed are nonprinting characters, so checking a variable for them is nontrivial. – Gordon Davisson Mar 22 '22 at 00:34
  • Thanks both! I do indeed have a \r in the $load variable. – Chait Mar 22 '22 at 00:44

1 Answers1

1

You can try this command to check $load :

printf "%q\n" "$load"

You might see :

$'5081.2\r'

where \r is the problem.

Update

In your case, this check is even better :

printf "%q\n" "$sql"
Philippe
  • 20,025
  • 2
  • 23
  • 32