Friday, July 15, 2011

Bash : syntax error in expression (error token is "12")

If you want to read a file line by line then you can follow the post in the link
When you echo this line to the display or to another file, then there seems to be no issues. But, when you try to store the line contents to a variable,
Eg:-
temp=$line
then you could get the error :-
syntax error in expression (error token is ".....")

This error occurs if the file, from which we are reading the lines, contains carriage return-line feed . The solution to this problem would be to strip the carriage return-line feed from the $line when it is being assigned to temp variable.
Solution :-
temp=${line//$'\r'}

Remember, if you want to assign temp again to another variable, then too you have to follow the same syntax of assignment as given in the solution.

No comments :

Post a Comment