cp doesn't work in script but works in terminal

5,745

Solution 1

Try using $HOME/.drush... instead of ~/.drush.... the "~" does not seem to be expanded to your home directory.

Solution 2

The tilde ~ character to mean the home directory only works at the beginning of a word, at the beginning of a value being assigned, or (for the purposes of PATH assignments) after a colon in a value being assigned. It must not be quoted.

Since ~ is expanded by the shell, the fact that you see it reported by cp means that you have a shell expansion problem.

Here the tilde is within double quotes, so it isn't expanded. Use either of these:

DRUSH_ALIASES_PATH=~/".drush/${PROJECT_NAME}.aliases.drushrc.php"
DRUSH_ALIASES_PATH="$HOME/.drush/${PROJECT_NAME}.aliases.drushrc.php"
Share:
5,745

Related videos on Youtube

Łukasz Zaroda
Author by

Łukasz Zaroda

Updated on September 18, 2022

Comments

  • Łukasz Zaroda
    Łukasz Zaroda over 1 year

    Now, this is a strange problem, I have this kind of script:

    CWD="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
    RESOURCES_PATH="${CWD}/resources"
    
    PROJECT_NAME="something"
    
    DRUSH_ALIASES_EXAMPLE_PATH="${RESOURCES_PATH}/example.aliases.drushrc.php"
    DRUSH_ALIASES_PATH="~/.drush/${PROJECT_NAME}.aliases.drushrc.php"
    
    cp ${DRUSH_ALIASES_EXAMPLE_PATH} ${DRUSH_ALIASES_PATH}
    echo "cp ${DRUSH_ALIASES_EXAMPLE_PATH} ${DRUSH_ALIASES_PATH}"
    

    When I'm trying to run that kind of script, I'm getting error: "cp: Cannot create regular file "~/.drush/something.aliases.drushrc.php". There is no such file or directory"

    But the funny thing is, that if I will copy the output of "echo" and paste in directly into terminal, the command will work just fine. I'm confused, any ideas what can be wrong with above script?

  • derobert
    derobert over 10 years
    Yep, because ~ is not expanded when quoted. Of course, the arguments to cp really ought to be quoted as well, to avoid surprises.
  • Mark Plotnick
    Mark Plotnick over 10 years
    ~/"quoted name" will work, as well.