I'm using, (uname -a says) Linux ip-x-x-x-x #1 SMP Tue Sep 1 10:25:30 EDT 2009 x86_64 GNU/Linux.

I've got a script, foo.sh with 100 lines of code and when I run

head -n 5 foo.sh

the command just "hangs" until I press Ctrl-C.

man head

doesn't give much insight.

The path to head (via which) is /usr/bin/head

I'm SSH'd into the server if that makes a difference.

My shell is /bin/bash.

A workaround to use

sed 5q foo.sh

works great, but I'd like an answer to this question. Thanks everyone.

  • what does head --version report? Does head -n 5 < foo.sh work as expected? If so, after head -n 5 foo.sh, does typing a few lines of random text followed by ctl-D cause the first 5 lines of your random text to be echoed? – rici Sep 18 '13 at 19:52
  • @rici head --version hangs, too! head -n 5 < foo.sh doesn't work. I tried typing anyway and my characters get echoed immediately. – jcarpio Sep 18 '13 at 20:36
  • If head --version hangs (and you have no other problems, tail or similar programs work etc) then this is, as @rici said, almost certainly not a normal head but something else. – terdon Sep 18 '13 at 23:46

I was going to suggest that your head is symlinked to busybox, but even busybox will work with head -n 5 < foo.sh. (In fact, the one I tried works with head -n 5 foo.sh, but maybe there are versions which don't.) Still, it seems likely that the head on your machine is not any standard head. Perhaps its an alias, or a shell function.

Small note: It's important (although not always obvious) to distinguish between a program that is "hanging" and a program that is simply waiting for user input. Yours is waiting for user input. That's different from, for example, sleep 3600 or for ((i=1;i<100000000;++i)); do :; done.

Here are some things you can try:

$ type head
head is aliased to `echo Surprise\!'

$ type head
head is a function
head () 
    echo surprise\!
# ...

$ type head
head is hashed (/usr/local/bin/head)
$ file /usr/local/bin/head
/usr/local/bin/head: POSIX shell script, ASCII text executable

$ type head
head is hashed (/usr/local/bin/head)
$ file /usr/local/bin/head
/usr/local/bin/head: symbolic link to `/bin/busybox'
  • Ah, file head gives this information. ip-x-x-x-x:/usr/bin# file /usr/bin/head /usr/bin/head: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.8, stripped – jcarpio Sep 23 '13 at 21:36
  • @jcarpio: Gosh, your linux kernel just celebrated its ninth birthday! Did hash head reveal that it was using that path? Investigating oddities like this should not be done from a root account, by the way. If an attacker had gotten into your system and injected nasties over top of your standard utilities (or taken advantage of a badly-configured $PATH), running them as root would be playing into their hands. – rici Sep 23 '13 at 21:45
  • well, hash head doesn't give anything. at this point, the sed workaround seems fine. thanks for the help! – jcarpio Sep 25 '13 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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