Frank P.
Frank P.

Reputation: 513

Bash scripting - Asking user for input file, how to make tab-completion work?

I'm writing a simple bash script where I ask a user for an input file to then execute.

I am currently using the read -p command. It fails, however, to work with the built in completion for file names/directories in unix. Everytime I hit Tab while at this prompt, my terminal just skips spaces similar to Tab functionality in a text editor. Is there a way for it to incorporate this?

Upvotes: 12

Views: 5961

Answers (1)

that other guy
that other guy

Reputation: 123550

Use -e:

#!/bin/bash
read -e -p "Enter filename, use tab for completion: " file
ls -l "$file"

-e uses the readline library to read input just like bash does for its prompt. This allows not only filename completion but also using arrow keys, home/end, vi editing and similar goodness.

Upvotes: 23

Related Questions