Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 6 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? –  Tor Thommesen Jun 24 '13 at 20:13
    
It does work in Debian, which is where I plan to use it. :) –  Tor Thommesen 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
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. –  Stephane Chazelas 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/" –  glenn jackman Jun 24 '13 at 20:27
show 3 more comments

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.