read command doesn't wait for input

mohammadh montazeri picture mohammadh montazeri · Apr 1, 2013 · Viewed 17.8k times · Source

I have problem executing a simple script in bash. The script is like this:

#! /bin/sh

read -p 'press  [ENTER]  to continue deleting line'
sudo sed -ie '$d' /home/hpccuser/.profile

and when I execute the script with ./script the output is like this:

press  [ENTER]  to continue deleting line./script: 3: read: arg count
[sudo] password for user

I run the read command directly in terminal (copy and paste from script to terminal) and it works fine; it waits for an ENTER to be hit (just like a pause).

Answer

Charles Duffy picture Charles Duffy · Apr 1, 2013

Because your script starts with #!/bin/sh rather than #!/bin/bash, you aren't guaranteed to have bash extensions (such as read -p) available, and can rely only on standards-compliant functionality.

See the relevant standards document for a list of functionality guaranteed to be present in read.

In this case, you'd probably want two lines, one doing the print, and the other doing the read:

printf 'press [ENTER] to continue deleting...'
read _