7

I am creating a script which does some processing on a file. For the file, Ι will select any file from the current directory or will give path to a file.

Problem is the file name can be huge, so typing it all will be a pain.

echo "Enter file name"
read FILE

The above code demonstrate what I am trying to do. When running this script, I have to put the file name character by character, which will be a huge pain. I want to get suggestions like in any terminal application by pressing Tab.

I want something like this Something like this to show up on pressing Tab while writing the file name. How can I achieve this?

2
  • 2
    Why not just pass the filename on the command line? It's a bit more Unix-y, and you get tab completion for free... Jun 9, 2022 at 19:56
  • 1
    @RogerLipscombe that's a good idea too, but some times i have to choose the file depending on how script is going.
    – tycoon
    Jun 10, 2022 at 11:38

1 Answer 1

10

You can read the input filename like this:

#!/bin/bash
read -e -p "Enter filename: " f
printf "filename: %s\n" "$f"

read -e uses readline which has already tab-completion for paths. read --help and man readline for details about them.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .