Bash脚本 - 〜/ .ssh /中没有文件

spb*_*pbr 0 linux bash quoting tilde-expansion

我正在尝试复制一个文件:〜/ .ssh /但每次我运行脚本时都会说

pi@raspberrypi:/etc/greenwich $ ./copybash.sh
cat: ~/.ssh/testfilegen2.log: No such file or directory
Run Code Online (Sandbox Code Playgroud)

copybash.sh

!/bin/bash
sourceFile="~/.ssh/testfilegen2.log"
targetFile="/etc/network/interfaces2"
sudo cat "$sourceFile" > "$targetFile"
sudo service networking restart
Run Code Online (Sandbox Code Playgroud)

有什么建议?

谢谢

che*_*ner 5

取消指定赋值中的波浪号sourceFile,使其正确展开.参数扩展时不会发生Tilde扩展.

sourceFile=~/".ssh/testfilegen2.log"
Run Code Online (Sandbox Code Playgroud)

(在这种情况下,根本不需要引号,只是为了证明~以下/是唯一需要保持不引用波浪扩展的东西.)