[Solved] Sed Failed to Reference Variable error: extra characters after command


Operating system: CentOS
Background: shell script


sudo sed -n '842,${line}p' /.../file > ./file1

After execution, an error is reported:
– e expression # 1, char 7: extra characters after command.


After investigation, it is found that it is OK to replace single quotation marks with double quotation marks, as follows:

sudo sed -n "842,${line}p" /.../file > ./file1

If none of the methods found work, try this one.

Remember that variables cannot be referenced within single quotes.


Read More: