Bash:将变量中的文件夹名与文件名合并

per*_*sec 5 bash shell-script quoting command-substitution

首先,我写了一个配置文件,其中包含我所有的参数,如下所示

path="/home/test/"
Run Code Online (Sandbox Code Playgroud)

我的名字test.conf

然后我用这个内容编写一个 shell 脚本,命名它test,并用chmod +x.

#!/bin/bash
#read the config file
. /home/test/test.conf

#cat the file /home/test/test
cat `$path`test #This line is the problem
Run Code Online (Sandbox Code Playgroud)

我得到这个输出

./test/test: line 3: /home/test/: Is a directory
cat: test: No such file or directory
Run Code Online (Sandbox Code Playgroud)

我想要的是它向我显示文件的内容/home/test/test

如何正确编写此脚本,使其不会在文件路径后换行?

rus*_*ush 13

``$()用于命令执行,而不是用于替换变量内容。所以 bash 尝试执行 varaible 含义 in``并返回它是目录的错误。

只需编写cat ${path}test,它就会以您想要的方式工作。

有关bash 变量命令替换的更多信息,请阅读。