8

I want to make a script that: (1) Gets a path giving the user the ability to use tab to autocomplete, then (2) gets a filename from the user, and then (3) creates a file at that path with extension .txt.

My question is: How do I do 1.? How do I tell bash to ask for input that is an "autocompletable" path, preferably with part of the path (say ~/x/) already filled out by default?

Note: the paths will almost always include spaces.

16
read -p "enter a directory: " -i "$HOME/x/" -e path

From a bash session, enter help read|less

7
  • Thanks! On osx I get this error: "-bash: read: -i: invalid option read: usage: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...]" Is this an osx thing?
    – user36719
    Jun 24 '13 at 20:13
  • It does work in Debian, which is where I plan to use it. :)
    – user36719
    Jun 24 '13 at 20:17
  • you probably have an older version of bash on your mac: echo $BASH_VERSION Jun 24 '13 at 20:22
  • 2
    The -i flag was added in bash 4.0. help '[r]ead' to get the help for read only. Note that while read -e will be able to complete inside ~, it will not be expanded. Jun 24 '13 at 20:23
  • 2
    It means you'll get the literal tilde in the $path variable, which may be difficult to handle. In hindsight, use -i "$HOME/x/" Jun 24 '13 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy