3

I was looking inside /etc/skel/.bashrc and found this line:

[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

I understand what the test -x does, but everything onward with eval is not-understandable.

Further more, when I run this line in other Linux distros it will cause an error:

#  eval "$(SHELL=/bin/bash lesspipe)"
bash: syntax error near unexpected token `newline'

This is on Ubuntu 14.04 (bash 4.3.11(1)), and the other bash version I tested is 4.3.39(1).

Can you explain what this line is doing, and why am I getting the above error?

2

To understand the eval snippet eval "$(SHELL=/bin/sh lesspipe)", we need to break it down:

  • At first let's check what SHELL=/bin/sh lesspipe returns:

    $ SHELL=/bin/sh lesspipe
    export LESSOPEN="| /usr/bin/lesspipe %s";
    export LESSCLOSE="/usr/bin/lesspipe %s %s";
    

    As you can see it returns two commands containing variable declarations preceded by export to make them environment variables.

  • Now another thing, $() is the command substitution syntax that returns exactly what we have seen above i.e. the output of a command, let's check:

    $ echo "$(SHELL=/bin/sh lesspipe)"
    export LESSOPEN="| /usr/bin/lesspipe %s";
    export LESSCLOSE="/usr/bin/lesspipe %s %s";
    
  • Ok, what about eval now, so eval will just run the two commands returned by command substitution:

    eval "$(SHELL=/bin/sh lesspipe)"
    

    after this the variables LESSOPEN and LESSCLOSE will be environment variables to the shell on which this command is being run.

3
  • ok, this still does not explain error. Further more, when I do your first step I get what you say with /bin/sh (dash) but not with bash... – Oz123 Jan 22 '16 at 18:32
  • @Oz123 lessfile script comes with less..i can not reproduce the error in dash, ksh, bash, zsh ..On other systems this can behave differently of course, hard to say without peeking.. – heemayl Jan 22 '16 at 18:41
  • I already figured out that lesspipe which comes in less-481, packaged in gentoo is substantially different from the one which comes with Ubuntu (less 458-2). The one from gentoo can be found here gitweb.gentoo.org/repo/gentoo.git/tree/sys-apps/less/files/…, and there is even this one github.com/wofr06/lesspipe/blob/lesspipe/lesspipe.sh. Go figure, I always thought my bashrc is universal. Apparently it isn't... – Oz123 Jan 22 '16 at 18:46

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.