Add file/directory auto-completion to user input in Bash

19 Apr 2017

The most common way to get input from a user in a Bash script is to use the read function like so:

#!/bin/bash
echo "Type a word and press enter"
read INPUT
echo "Your word was $INPUT"

However, this is not the best method to use when you want the user to input a directory path. This is because pressing tab enters a literal tab instead of auto-completing the directory path like what happens in the terminal.

To add directory auto-completion to your bash script, all you have to do is add the -e flag to the read function like so:

#!/bin/bash
echo "Type a file or directory (use tab to autocomplete path!) and press enter"
read -e  INPUT
echo "Your path was $INPUT"

Thanks for reading!

If you liked that post, follow me on twitter to hear when I post new articles.