VideosMy ProjectsProgramming BlogGallery
  1. Home
  2. Blog
  3. Ensure relative paths work in a bash script
Programming Blog

Atom/RSS Feed

bash

Ensure relative paths work in a bash script

bash

To ensure relative paths work in a bash script you can set the working directory to the directory the script is in.

Setting the directory

To change the directory to the script location can you cd (change directory) to the value of dirname $0.

cd $(dirname $0)

Why this is important

This means that if the script is called from a different path the relative paths inside the script will still work.

Join my mailing list