0

I would like to write a script which add/create the file (like touch does). But, something own. First lines will be variables. Where:

echo -n "Choose your folder to save the file: "
read dir
echo -n "What is the name of your file: "
read name

etc. But in this case I should type a full path, but I want to use TAB button that complete queries.

I just want to make a TAB button works in my script. Like it works with cat, cd and with so many other commands.

Thanks in advance.

6
  • Please, clarify what are you trying to do, what is the goal. Adding an example will be helpful Jun 28, 2020 at 15:19
  • I would like to write a script which add the file (like touch does). But, something own. First lines will be variables. Where: echo -n "Choose your folder to save the file: "; read dir. But in this case I should type a full path, but I want to use TAB button that complete queries.
    – J. Doe
    Jun 28, 2020 at 15:23
  • Please edit your post to clarify Jun 28, 2020 at 15:23
  • You can enable line editing in the bash read builtin by adding the -e flag ... perhaps that's what you are missing? Jun 28, 2020 at 15:26
  • TAB is not working in my scripts. Like it works on cd, for example
    – J. Doe
    Jun 28, 2020 at 15:29

1 Answer 1

0

You want tab-completion to work when entering input to Bash's read? Then read -e is what you're looking for (manual):

Options: -e use Readline to obtain the line in an interactive shell

e.g.

/tmp/foo$ touch abcdef abcghi
/tmp/foo$ read -e -p 'enter a filename: '
enter a filename: ab[tab]
abcdef  abcghi  

Though you really should make the script accept input as command line arguments instead. That way, it's easier to run it from other scripts, or from shell loops etc.

Something like this, $1 is the first argument to the script, $# holds the number of arguments.

#!/bin/bash
if [ "$#" != 1 ]; then
        echo "Usage: $0 <filename>"
        exit 1
fi
echo "Doing something with '$1'..."

You must log in to answer this question.

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