I need an interactive bash script template to copy files to predefined destination folders. If possible, when prompted for user input tab completion would be enabled for a predefined source directory. The basic logic would go something like this:

script example 1:

enable tab completion to load from /src/dir #perhaps using pwd as script is loaded    
echo "filename or pattern?" #interactively ask user to provide filename or pattern    
'user input'=$FILENAME #define interactive user input as filename    
rsync -rv /src/dir/$FILENAME /dest/dir/ && rm -r /src/dir/$FILENAME    

I know it's rough/incorrect but hopefully the desired outcome is clear enough. Thanks in advance for any guidance!! Links to specific bash scripting techniques would be much appreciated.

I tried this (thanks for the help Noam N. Kremen and slhk!!!):

read -e -p "Filename or pattern? " SRC_FILE  
rsync "$SRC_FILE" /dest/dir && rm -r "$SRC_FILE"

So, tab completion works great, and after "quoting" the strings


rsync stopped throwing errors. Looks like I now have a great little script doing EXACTLY what I need it to!! And I learned how to format code correctly on here, Thanks!!

  • Please indent code by 4 spaces. What file are you copying? Does it contain a space in its name or path? Try quoting rsync "$SRC_FILE", and see mywiki.wooledge.org/Quotes – slhck May 1 '13 at 11:36
  • Thank you very much sir, problem fixed, and looking forward to perusing your site for more tips. – njtuneguy May 1 '13 at 12:03
  • No problem. I think at this point you should accept Noam's answer below, by clicking the green checkmark next to it, as he helped you figure out the solution. And welcome to Super User, of course! – slhck May 1 '13 at 12:05

try read -e -p as in:

read -e -p "Enter destination filename" DEST_FILE

-e enables autocompletion and $DEST_FILE will hold the data you need.

| improve this answer | |
  • Fantastic and quick help, thank you my friend! – njtuneguy May 1 '13 at 12:14

Your Answer

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

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