I have a bash script that looking at folder and copy files from one folder to another. The scripts is big enough, but here is just a two lines from it:
echo cp $working_directory/$folder_name/$f $new_filename
# cp $working_directory/$folder_name/$f $new_filename
Here is output of echo command:
cp ~/MEGAsync/development/experiments/bash_renamer/tres/a.pdf ~/MEGAsync/development/experiments/bash_renamer/tres_1.pdf
cp ~/MEGAsync/development/experiments/bash_renamer/tres/b.pdf ~/MEGAsync/development/experiments/bash_renamer/tres_2.pdf
I can launch any of this command and it works fine in the terminal.
But if I uncomment the real copy command and launch the script I will get the error:
cp ~/MEGAsync/development/experiments/bash_renamer/tres/a.pdf ~/MEGAsync/development/experiments/bash_renamer/tres_1.pdf
cp: ~/MEGAsync/development/experiments/bash_renamer/tres/a.pdf: No such file or directory
cp ~/MEGAsync/development/experiments/bash_renamer/tres/b.pdf ~/MEGAsync/development/experiments/bash_renamer/tres_2.pdf
cp: ~/MEGAsync/development/experiments/bash_renamer/tres/b.pdf: No such file or directory
Why I have this error and how can I fix it ?
~with"$HOME"~, and tilde expansion doesn't happen in quotes. If you didworking_directory=~/or just~/$folder_name/$fit should work. Also, unrelated to the problem, but you really should be quoting your variables to prevent word splitting issues in the future.