我正在尝试编写一个名为updateFile.sh的bash脚本。目前的情况如下:
function insertIntoFile(){
local variable=$1
local target=$2
echo "${variable}" | tee -a "${target}";
}
insertIntoFile "export/ foo=bar" "~/.bash_profile"运行bash -x ./updateFile.sh
为了输出我得到..。
...
+ echo 'export/ foo=bar'
+ tee -a '~/.bash_profile'
export/ foo=bar但是,当我cat ~/.bash_profile
它仍然是空的给定的字符串。
我尝试过没有斜杠的导出,所以我知道不是这样的,我在堆栈溢出中进行了挖掘,我看到的一切似乎都表明这应该有效,但是我不知道为什么不行,也不知道如何修复它。
发布于 2021-08-18 01:33:34
正如Inian评论的那样,您在双引号中使用“倾斜”:
insertIntoFile "export/ foo=bar" "~/.bash_profile"由于~没有在双引号内扩展而感到惊讶。使用$HOME变量或删除双引号:
insertIntoFile "export/ foo=bar" "$HOME/.bash_profile"或
insertIntoFile "export/ foo=bar" ~/.bash_profile此外,我还建议使用printf而不是echo,因为echo的各种实现都可能试图解析(阅读: mangle)一个以-n或-e开头的"$variable“值。更改这一行:
echo "${variable}" | tee -a "${target}";对此:
printf '%s\n' "${variable}" | tee -a "${target}";https://stackoverflow.com/questions/68823553
复制相似问题