“bad variable name” using “read var”

拈花ヽ惹草 提交于 2019-12-01 08:49:40
Andras Deak

There are most probably carriage returns (CR, '\r') at the end of your lines in the shell script, so the variable is trying to be read into var\r instead of var. This is why your error message is meaningless. A similar error should look like this:

run.sh: 3: export: [... the variable name]: bad variable name

In your case bash throws an error because var\r is illegal for a variable name due to the carriage return, so it prints

test.sh: 3: read: var\r: bad variable name

but the \r jumps to the beginning of the line, overwriting the start of the error message.

Remove the carriage returns fom the ends of lines, possibly by using the dos2unix utility.

Chandrani H

Here are a list of editors that support unix newline character.

Brackets has an extension for new-line/end-of-line support and it is built-in in Notepad++. Go to the 'Edit' tab. Find 'EOL Conversions' and select Unix (LF). That should get it done.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!