0

I am using the following executable to download a zip from the web, extract it and then delete the zip.

#!/usr/bin/env bash

echo "Downloading project resources from Storage..."

filename="outfile.zip"

if curl --silent -o "${PWD}/${filename}" -L "https://mylink.com"; then
    unzip "${filename}"
    if [[ -f "${PWD}/${filename}" ]]; then
        echo "Removing the file.."
        rm -f "${PWD}/${filename}"
    fi
else
    echo "Something went wrong"
fi
echo "Done!"

However, PWD does not seem to work as the zip is downloaded in the home directory of my Mac and the files are extracted there as well. I want to the zip to download and extract in the same folder of the executable file, no matter where it is.

What am I doing wrong here?

John Doe
  • 440
  • 4
  • 16
  • 1
    `$PWD` is not the directory the script is in, it's the working directory of the process running the script. That working directory is generally inherited from whatever process spawned the script. If you run the script from a Terminal window, it'll be whatever directory you were in when you ran the script. If you run it with something like `cron`, it'll generally be your home folder. If you want the script to work in its own directory, see [this question](https://stackoverflow.com/questions/59895/get-the-source-directory-of-a-bash-script-from-within-the-script-itself). – Gordon Davisson Apr 03 '19 at 00:41
  • 1
    ...so this looks like a misunderstanding of the concept of "current folder". The answers in the linked duplicate suggesting use of `$BASH_SOURCE` should be preferred over those relying on `$0`; see [BashFAQ #28](https://mywiki.wooledge.org/BashFAQ/028) for details. – Charles Duffy Apr 03 '19 at 00:44

0 Answers0