简体   繁体   中英

Can't read to var in Bash

I wrote a little Bash script and I'm having a problem while reading from the command line. I think its because I wrote the script on Windows. Here is the code:

read NEW_MODX_PROJECT

and the output of the debug mode

+ read $'NEW_MODX_PROJECT\r'

Finally here the error I get

': Ist kein gültiger Bezeichner.DX_PROJECT

I think in English it should mean "': is not a valid identifier.DX_PROJECT"

While writing it on Windows, it worked fine. I used console2 to test it which is using the sh.exe.

Your assertion is correct -- Windows uses CRLF line separators but Linux just uses a LF.

The reason for your strange error message is that while printing the name of your variable, it includes the carriage return as part of its name -- the terminal then jumps back to the first column to print the rest of the error message (which overwrites the beginning of the message with the end of it).

There are a set of utilities known as dos2unix and unix2dos which you can use to easily convert between formats, eg:

dos2unix myscript.sh

If you don't happen to have them, you can achieve the same using tr :

tr -d '\r' < myscript.sh > myscript-new.sh

Either will strip all the carriage returns and should un-confuse things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM