Question

I have written a bash script which is dependent on current folder structure, What should I do to make it runnable in any other folder(become portable)? let me explain more: I want the script to delete all the files in current directory that the script is running, or to delete all the files that are in the parent folder, what is the solution?

Was it helpful?

Solution

cd "$(dirname "$0")"

Add this to the top of your script to have it cd to the directory the script is installed at. Then you can use relative paths and they'll be relative to the script dir and not the user's pwd.

If for whatever reason dirname isn't available, here's an equivalent statement that uses basename instead.

cd "${0%%/$(basename "$0")}"

OTHER TIPS

Don't use absolute paths in your script.

we need more information to give you a proper answer...BUT some tips if you need to make it portable are to store any files that you need on a network share and simply make a new folder off of / such as /temporaryScriptFolder/ and mount the network share to that empty folder, allowing you easy access to any resources that are necessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top