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.

3
  • 1
    I suspect one of your variables ($load?) contains a line feed. Commented Mar 22, 2022 at 0: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?" Note that both carriage return and linefeed are nonprinting characters, so checking a variable for them is nontrivial. Commented Mar 22, 2022 at 0:34
  • Thanks both! I do indeed have a \r in the $load variable. Commented Mar 22, 2022 at 0:44

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks .. yes I do have a "\r" in the $load variable!

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.