1

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 ?

4
  • 2
    Try replacing ~ with "$HOME" Commented Jul 25, 2014 at 18:59
  • In this case all works fine. But why it doesn't work with '~' ? Commented Jul 25, 2014 at 19:00
  • 3
    Most likely because you quoted ~, and tilde expansion doesn't happen in quotes. If you did working_directory=~/ or just ~/$folder_name/$f it should work. Also, unrelated to the problem, but you really should be quoting your variables to prevent word splitting issues in the future. Commented Jul 25, 2014 at 19:07
  • Its how bash interprets ~ sign. The same should work fine in c shell which I use it a lot. Commented Jul 25, 2014 at 19:26

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Tilde Expansion

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

Comments

0

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

2 Comments

Getting in to the habit of using evil, especially when equally simple solutions exist, is likely to cause security holes in the future.
I'm guessing that typo was intentional?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.