I have the following lines to prompt a user to enter a date/time that I use further down in my script:
printf "Please insert start of time window in format YYYY-MM-DD HH:MM:SS: \n"
read startDatetime
Is there a way I can have the "read" prompt insert in the dashes for the date, the space between the date and time, and the colons for the time dynamically as a user types? I know this is possible using JavaScript on the web, but I am specifically looking for a command line version of that functionality.
I searched the web for an answer to this and cannot find one. I see an option for delimiter in the read Man Page but cannot see how it would achieve what I am trying to do.
You can cobble it together by limiting the number of characters you read at a time using the
-Nflag forread:Looks like this:
Alternatively, with the option to move the cursor back past the automatically added
-, blank, and:: we can usetputto move the cursor, clear the line, and then useread -e(use readline for input) and-i(prefill prompt):This isn't perfect, either: the extra characters are only inserted once, but not when you go back to fix something; and there's a newline inserted after the prompt that results in a blank line, which would be noticeable at the bottom of the terminal emulator:
At this point, you might consider using something like charmbracelet/gum for a very user-friendly interactive input experience at the terminal.