“bad variable name” using “read var”

前端 未结 2 795
清歌不尽
清歌不尽 2021-01-12 20:13

I am getting confused about Linux shells. It may be that I oversee something obvious as a Linux noob.

All I want is the following script to run:

#!/b         


        
相关标签:
2条回答
  • 2021-01-12 20:59

    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.

    0 讨论(0)
  • 2021-01-12 21:17

    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.

    0 讨论(0)
提交回复
热议问题