Reputation: 45325
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 ?
Upvotes: 1
Views: 339
Reputation: 5422
This is not working because, most likely, in your $working_directory
variable definition you have the tilde ~
quoted, thus the tilde expansion in bash is not working.
If a word begins with an unquoted tilde character (‘~’), all of the characters up to the first unquoted slash (or all characters, if there is no unquoted slash) are considered a tilde-prefix. If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the tilde are treated as a possible login name.
~ The value of $HOME ~/foo $HOME/foo
Upvotes: 2
Reputation: 1547
It looks like the tilde ~
that is part of the variable is not being expanded.
Maybe try doing this first?:
eval working_directory=$working_directory
Upvotes: 0
Reputation: 881
I think that the problem is with the ~
sign. If the echo prints it as it is, it means, that it looks for directory with name ~
in the working dir. You can replace ~
with $HOME
or try to execute the command some different way.
Upvotes: 3