cat brain.log | less

Getting it down on `paper`

Get the Directory Path of a Bash Script

So you have a bash script and you want to run it from crontab or some other context where the current working directory isn’t known. Your script wants to use relative path information (from the script itself), perhaps to create a directory structure in the script’s base directory.

The question is: How do you get the directory path of the script? dirname? Not reliably.

#!/bin/bash
ABSPATH="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")"
CWD=`dirname "$ABSPATH"`

Nice!

 

Comments

No comments so far.

(comments are closed)