Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 12 down vote accepted
read -p "enter a directory: " -i "$HOME/x/" -e path

From a bash session, enter help read|less

share|improve this answer
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 – glenn jackman Jun 24 '13 at 20:22
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. – Stéphane Chazelas Jun 24 '13 at 20:23
It means you'll get the literal tilde in the $path variable, which may be difficult to handle. In hindsight, use -i "$HOME/x/" – glenn jackman Jun 24 '13 at 20:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.