Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two following linux shell commands:

echo 'CPU type and model'
cat /proc/cpuinfo | head -5 | tail -1

When i run these commands directly in Terminal, i get this result truly:

CPU type and model
model name  : Intel(R) Core(TM) i3 CPU       M 330  @ 2.13GHz

But when i save them into a test.sh file and run sh test.sh, i get this error:

CPU type and model
tail: option used in invalid context -- 1

I also try bash test.sh and get this:

CPU type and model
tail: option used in invalid context -- 1
share|improve this question
Slightly off-topic, but you've got yourself a nice useless use of cat there. Wouldn't it be simpler to simply write head -n 5 /proc/cpuinfo | tail -n 1? – rahmu Apr 23 '12 at 6:48
I test it and get : invalid number of lines error. – Navid Farhadi Apr 23 '12 at 6:50
Apparently your versions of head and tail don't support the -n options. That was not my point. You could try head -5 /proc/cpuinfo | tail -1 – rahmu Apr 23 '12 at 7:07
up vote 2 down vote accepted

You have carriage returns and possibly other extraneous characters in your script file; use dos2unix to remove them.

share|improve this answer
Thanks, I remove ; from end of first line, and one of errors fixed, but not all of them. – Navid Farhadi Apr 23 '12 at 6:23
I think your current error is because legacy pre-POSIX options don't always work (I wonder about the shell and/or PATH you're using that does that, though). tail -n1 and head -n1 are the POSIX versions of tail -1 and head -1. – geekosaur Apr 23 '12 at 6:29
I change my script to tail -n1 and i get : invalid number of lines error. – Navid Farhadi Apr 23 '12 at 6:33
I wonder if you still have extra characters, then (garbage at the end of the option). Use cat -v to check for control characters in the script. – geekosaur Apr 23 '12 at 6:38
That was where this response started... ^M is not a valid line ending character on Unix-like systems. Use dos2unix to reformat the line endings without the ^M, or tr -d '\r' or some other mechanism to remove them. – geekosaur Apr 23 '12 at 6:50

Your Answer


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.