shell reads file created in windows incorrectly

后端 未结 2 800
借酒劲吻你
借酒劲吻你 2021-01-14 12:47

Here is my shell script and properties file

test.sh

#!/bin/bash
source ./default.properties
echo $app_name
echo \"$app_name ==\"

de

相关标签:
2条回答
  • 2021-01-14 13:06

    As tripleee already mentioned it is a problem with the different encoding in windows and linux. You could also encounter problems with special chars like "äöüß"

    You can use the linux tool "recode".
    Example: change encoding from ibmpc (DOS with cr-lf to latin)

    recode ibmpc..lat1 test.sh
    

    See all supported charsets:

    recode --list
    
    0 讨论(0)
  • 2021-01-14 13:26

    Yes, you have a DOS carriage return at the end of the line which assigns the appName variable. This control character causes the cursor to move back to the beginning of the line in most terminals.

    Good catch; most askers here don't even realize this is a problem. The bash tag wiki has a section about troubleshooting this issue.

    Many tools on Unix will show the problematic character as ^M (control-M) or its octal code \015; in hex it's character code 0x0D and in decimal it's 13.

    In the future, if you absolutely have to use a Windows editor, save with Unix line endings, transfer the file using ftp ASCII mode (not a popular or safe option these days), or run dos2unix on the file on Unix after transferring it.

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