Home > Enterprise >  Bash - setting a string variable and printf
Bash - setting a string variable and printf

Time:03-22

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.

CodePudding user response:

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"

CodePudding user response:

Replace $load with ${load%$'\r'} to remove carriage return in output.

  • Related