Search code examples
linuxbashquotingtilde-expansion

Bash Script - No file exist in ~/.ssh/


I'm trying to copy a file from: ~/.ssh/ but everytime I run the script it keeps saying

pi@raspberrypi:/etc/greenwich $ ./copybash.sh
cat: ~/.ssh/testfilegen2.log: No such file or directory

copybash.sh

!/bin/bash
sourceFile="~/.ssh/testfilegen2.log"
targetFile="/etc/network/interfaces2"
sudo cat "$sourceFile" > "$targetFile"
sudo service networking restart

Any Suggestions?

Thank you


Solution

  • Unquote the tilde in the assignment to sourceFile so that it expands properly. Tilde expansion does not occur on parameter expansion.

    sourceFile=~/".ssh/testfilegen2.log"
    

    (In this case, no quotes would be necessary at all, but just to demonstrate that the ~ and the following / are the only things that need to remain unquoted for tilde expansion to occur.)