DEV Community

Anthony Bruno
Anthony Bruno

Posted on • Originally published at anthonybruno.dev on

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

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"

Top comments (0)