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?
Explanation: read command is the shell's internal tool for taking input from the user i.e. it makes the scripts interactive.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With