0

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

Cyrus
  • 84,225
  • 14
  • 89
  • 153
spbr
  • 23
  • 6

2 Answers2

5

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.)

chepner
  • 497,756
  • 71
  • 530
  • 681
1

Take a look to this snippet code:

#!/bin/bash
v1=~/'file1.txt'
v2=~/'file2.txt'
echo 'Hi!' > $v1 
cat $v1 > $v2
cat $v2

$ script.sh
Hi!

The documentation is in the section "Tilde Expansion" of the "General Commands Manual BASH".

Grégory Roche
  • 162
  • 2
  • 9