Bash script to read file location

I'm writing a bash script that reads a file location from a user, and I'm wondering how to get the script to accept tab to auto complete the directories that are input.

Check if your version supports the -e option:

read -e ...

Thanks..

Another related problem:

I'm querying the user for a file location/name to create a file to log information to... something like /home/username/info.txt

But if the user enters ~/info.txt

the program errors saying the file does not exist?

Is there a way to get the read operation to accept ~?

Tilde expansion happens before parameter and variable expansion, so I suppose you should use something like this:

#!/bin/bash
 
read -ep'enter filename: '
 
ls -l "${REPLY/~/$HOME}"
 

So, my code is:

And the current output is:

How would I resolve this? (testDir does exist)

Also assume that the user will not always enter ~/...

---------- Post updated at 11:10 AM ---------- Previous update was at 10:20 AM ----------

I've attempted different variations on this to no success..

Try to change this:

echo -n > $file

to

echo -n > "${file/#\~/$HOME}"

Interesting..

care to explain how that works?

This is from man bash in the section Parameter Expansion:

The code substitutes the tilde in the begining of the pattern with the current value of the variable $HOME.

1 Like

Brilliant! I appreciate your help :]