Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This simple script should read one letter:

#!/bin/bash
read -n 1 key
echo $key

But it gives the following error

read: Illegal option -n

Executed by hand read -n 1 key works though.

share|improve this question
1  
How are you running that script? –  Mat Oct 27 '12 at 9:52
    
put type read in your script, and see what that is –  warl0ck Oct 27 '12 at 9:52
    
read is a shell builtin -n: not found 1: not found key: not found –  Davoud Taghawi-Nejad Oct 27 '12 at 10:44
    
sh; yes that solved it running it with ./script.sh works –  Davoud Taghawi-Nejad Oct 27 '12 at 10:48
    
Another shell runs it then — I guess you have some other sh bastard installed. –  lynxlynxlynx Oct 27 '12 at 21:45

1 Answer 1

The -n option to read is a bash extension, not found in some other shells. If you got the error read: Illegal option -n, it means you didn't run the script under bash. It's most likely that you ran the script under /bin/sh, and /bin/sh is not bash but dash (the error message is the one dash produces).

To run a script under bash, start it with a shebang line #!/bin/bash, make the script file executable, and call it by name. Since you have a shebang line, the problem may be that you ran . /path/to/script instead of /path/to/script from another script running under /bin/sh.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.