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.

The less command accepts its defaults with an environment variable LESS, so you can

export LESS='-F -g -i -M -R -S -w -X -z-4'

at the beginning of your session.

Is it possible to change the default lines count returned by head and tail in a similar fashion?

An alias is not an option, because it breaks explicit option setting (e.g. in a script):

head -n 15 -5

fails with an error in both GNU and busybox head at least.

share|improve this question
1  
What do you mean with "breaks explicit option setting"? What exactly will break if you set something explicitly? –  Anthon Mar 3 at 13:12
2  
From the info page for head you can read that using -5 is obsolete: For compatibility 'head' also supports an obsolete option syntax '-COUNTOPTIONS', which is recognized only if it is specified first. At least for head from coreutils. –  Anthon Mar 3 at 14:34
1  
Still, old scripts may be using that form and an alias breaks their execution. –  malteo Mar 3 at 15:13

3 Answers 3

up vote 4 down vote accepted

Since the old style options like -5, +5 are only recognised as the first argument, you could do:

head()
  case $1 in
    ([-+][0-9]*) command head "$@";;
    (*) command head -n 15 "$@"
  esac

That will affect the heads invoked by your current shell.

If you want to affect all head invocations, you'd need to write it as a script that appears first in your $PATH:

mkdir -p ~/bin && cat > ~/bin/head << \EOF &&
#! /bin/sh -
case $1 in
  ([-+][0-9]*) ;;
  (*) set -- -n 15 "$@"
esac
exec /usr/bin/head "$@"
EOF
chmod +x ~/bin/head
PATH=~/bin:$PATH export PATH
share|improve this answer

Use an alias, e.g. alias head="/usr/bin/head -n 15" (no, it will not break any later options).

Use a function, e.g.

head() {
  /usr/bin/head -n 15 "$@"
}

Cobble up your own, as the head(1) info for GNU suggests, i.e., use sed 15q in a script.

share|improve this answer
    
It breaks it in the case of head -n 15 -2 with both busybox and GNU head and tail at least –  Stéphane Chazelas Mar 3 at 14:00
1  
Breaks BSD head and tail shipped with OSX too –  malteo Mar 3 at 14:35

Using an alias is certainly an option if you are working with head from coreutils version 8.13. That is because

head -n 5 -n 15 file_name

will give you the first 15 lines of file_name (assuming that file has enough lines. The second -n option overrides the first.

So you can create an alias:

alias head="/usr/bin/head -n 5"

to set the default to five and then use:

head -n 15 file_name

The same holds true for tail.

share|improve this answer
    
It breaks it in the case of head -n 15 -2 with both busybox and GNU head and tail at least. –  Stéphane Chazelas Mar 3 at 14:01
    
Breaks BSD head and tail shipped with OSX too –  malteo Mar 3 at 14:31
2  
@StephaneChazelas -2 is obsolete syntax according to the head info page. head --help and man help` don't even mention that anymore. –  Anthon Mar 3 at 14:31
1  
Yes, but still used in many scripts. GNU once removed support for those but had to bring it back as it was causing outrage –  Stéphane Chazelas Mar 3 at 15:05

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.