Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

shell

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?

like image 601
Frank P. Avatar asked Oct 28 '14 21:10

Frank P.


People also ask

Which command is used for taking input from the user and make the scripts interactive?

Explanation: read command is the shell's internal tool for taking input from the user i.e. it makes the scripts interactive.

What command can be used to get input from the user of the script and assign that input to a variable?

The ACCEPT Command. The ACCEPT command is used to obtain input from the user. With it, you specify a user variable and text for a prompt. The ACCEPT command displays the prompt for the user, waits for the user to respond, and assigns the user's response to the variable.


1 Answers

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.

like image 197
that other guy Avatar answered Sep 29 '22 18:09

that other guy