Following the shell script that I am executing
#!/bin/sh
echo "Enter [y/n] : "
read opt
Its output is
Enter [y/n] :
Y
I want that the variable should be read on the same line like below
Enter [y/n] : Y
Should be simple I guess, but I am new to bash scripting.
The shebang
#!/bin/shmeans you’re writing code for either the historical Bourne shell (still found on some systems like Solaris I think), or more likely, the standard shell language as defined by POSIX. This means thatread -pandecho -nare both unreliable.The standard/portable solution is:
(The
-rprevents the special treatment of\, sincereadnormally accepts that as a line-continuation when it’s at the end of a line.)If you know that your script will be run on systems that have Bash, you can change the shebang to
#!/bin/bash(or#!/usr/bin/env bash) and use all the fancy Bash features. (Many systems have/bin/shsymlinked tobashso it works either way, but relying on that is bad practice, andbashactually disables some of its own features when executed under the namesh.)