CWD (current working directory) is incorrect when a bash scripts is double clicked. How can we fix this?

When we get started with bash scripting, one of the most frequent problems we come across is the incorrect CWD (current working directory). This is problematic when we expect to manipulate files whose location is being set with relative paths. To fix the problem you just need to add the following lines to top of your bash script:
#!/bin/bash
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd "${DIR}"
...your script code
If you found this post useful, leave a thumbs up 😊. See ya.

Leave a Reply

Close Menu