bashshelltilde-expansion

Stop tilde expansion in bash script arguments


Consider this tiny script:

# script.sh
echo $@

If I call it like this ./script.sh ~/docs, I get /home/me/docs as output. However, I need it to echo ~/docs. How can I achieve that?


Solution

  • It's not under the script's control. It's up to the user. They must quote the tilde to prevent it from being expanded. Options include:

    ./script.sh \~/docs
    ./script.sh '~/docs'
    ./script.sh "~"/docs