Trying to use the "read command" to accept user input from the command prompt itself but my script doesnt seem to be moving forward

echo "Do you want to continue?(yes/no)"
read -p $1
if [ "$1" == "yes" ]
sleep 5s
echo ""
echo " move ahead"
    echo ""
    echo "Skipping The Step.."
    echo ""
sleep 5s

I want to execute the script like this..

sh script.sh yes
sh script.sh no  

Added a -p to the above script and all seems to work very well. This is my real problem. I have another script test.sh which calls script.sh. So this is how i put the input

cat  test.sh yes
echo "execute the below script"
sh script.sh $1

sh test.sh yes  

This way doesnt work as the script picks up a default no and moves ahead. Any ideas.

  • I added a -p command in front of the script and it seems to work. This is my real problem. I have another script called test.sh which in turn calls this script ie script.sh. So in test.sh i added this line. cat test.sh yes sh script.sh $1 This was it doesnt seem to work
    – Super
    Jul 20, 2016 at 6:09
  • No, the -p argument doesn't even allow the user to change the answer. Jul 20, 2016 at 6:10
  • Do you want it as a command line argument or as user input? Jul 20, 2016 at 6:10
  • @JuliePelletier - looking at a command line input but only problem is one script has to call the other script like i showed it above.
    – Super
    Jul 20, 2016 at 6:16
  • bash is not sh. bash is the GNU implementation of a sh interpreter, but there are many other different implementations. read -p is not valid standard sh syntax. Those different sh implementations will treat it differently. Check here for the sh language specification, and specifically for the read utility. Jul 20, 2016 at 7:24

1 Answer 1


$1,$2 … - command line positional arguments and couldn't be assigned like read [-p] $1 or any other way except

set -- firsr_arg second_arg …

For your case it can be test if arguments is present then test them

while [ -z "$REPLY" ] ; do
    if [ -z "$1" ] ; then
         read -p "Do you want to continue?(yes/no) "
         set --
    case $REPLY in
        [Yy]es) sleep 5s
                echo -e "\n move ahead" ;;
         [Nn]o) echo -e "\nSkipping The Step..\n"
                sleep 5s ;;
             *) echo "Wrong answer. Print 'yes' or 'no'" 
                unset REPLY ;;
  • what set -- does ?
    – Rahul
    Jul 20, 2016 at 6:42
  • That is the obvious answer but so far I doubt it will clarify the confusion in OP's mind. Jul 20, 2016 at 6:42
  • 1
    @Rahul help set: Set or unset values of shell options and positional parameters. Change the value of shell attributes and positional parameters, or display the names and values of shell variables.
    – Costas
    Jul 20, 2016 at 6:54
  • @Costas cool, you have already earned my upvote ;)
    – Rahul
    Jul 20, 2016 at 6:59
  • That very much depends on the sh implementation. read 1 or 1=foo work in zsh. 1=foo also works in rc and derivatives. Note that read -p is not sh syntax. In some sh implementations, it's to read from the co-processes. Only the bash implementation of sh recognises it as the way to specify a prompt. In any case, none of read -p var, read 1 or read without variable is valid POSIX sh syntax. You want printf 'Prompt: '; read REPLY in a sh script. Jul 20, 2016 at 7:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .