arrow_back Back to Notebook
bash
bash
January 10, 2019

Ensure relative paths work in a bash script

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.

Thanks for reading!

John Wiseman

Full-Stack Software Engineer building scalable digital solutions. specializing in modern web technologies and mission-critical systems.

© 2026 Wiseman Systems Pty. Ltd.

Navigation

Start a Conversation

Ready to discuss your next project? Let's build something extraordinary together.

Get in Touch