简体   繁体   English

用 bash/shell 在 echo 中显示 2 个变量的疯狂问题

[英]crazy issue to display 2 variables in echo with bash/shell

I have a stupid problem completely crazy with shell/bash script.我对 shell/bash 脚本有一个愚蠢的问题。 I have a configuration file with 2 variables defined...See below my file called test.conf :我有一个定义了 2 个变量的配置文件......请参阅下面我的名为test.conf 的文件:

WEB_DIR="www"
TPL_DIR="tpl"

Now I have my shell/bash script called test.sh with this content :现在我有一个名为test.sh 的shell/bash 脚本,其中包含以下内容:

#!/bin/bash
source test.conf
echo $WEB_DIR
echo $TPL_DIR
echo -e "$WEB_DIR - $TPL_DIR"
exit 0

As you can seen this is very simple because this kind of script should return this :如您所见,这非常简单,因为这种脚本应该返回:

www
tpl
www - tpl

But, I don't know why, when I launch this script, he returns this :但是,我不知道为什么,当我启动这个脚本时,他返回:

www
tpl
- tpl

We can see that the first variable $WEB_DIR is missing and I don't know why because she is defined.我们可以看到第一个变量$WEB_DIR丢失了,我不知道为什么,因为她被定义了。

Do you have an idea why?你知道为什么吗?

Thanks L.谢谢L。

test.conf has DOS line endings, so the value of each variable ends with a carriage return. test.conf有 DOS 行结尾,所以每个变量的值都以回车结束。 Compare:相比:

$ WEB_DIR=$'www\r'
$ TPL_DIR=$'tpl\r'
$ echo -e "$WEB_DIR - $TPL_DIR"
 - tpl

The carriage return at the end of WEB_DIR is "displayed" by moving the cursor to the beginning of the line, so that - tpl overwrites www . WEB_DIR末尾的WEB_DIR是通过将光标移动到行WEB_DIR来“显示”的,这样- tpl覆盖了www

Remove the DOS line endings with dos2unix , or use your text editor to save the file using Unix line endings.使用dos2unix删除 DOS 行结尾,或使用文本编辑器使用 Unix 行结尾保存文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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