For simple commands like less, is it better to make an alias or to export the options? Is there any benefit of one over the other?

For example, if I want less to always use the -R option to show raw characters, both of these solutions work:

  1. export LESS='-R'
  2. alias less='less -R'

For a simple case like this, is there any benefit of one over the other? Any scenarios where one will cause problems and the other won't?

I've read through several info and man pages, as well as Googling it, but I'm still stuck on this.

  • 1
    No it really comes down to personal preference. I voted to close this since it seems more opinion based. – slm May 16 '14 at 1:28
  • Three more solutions: create a shell function (only works in the shell but need not be interactive); create a script in ~/bin or wherever (has shell startup overhead but works immediately in running shells and from other programs, and is persistent); program-specific config files (for less you can do it using lesskey) – deltab May 16 '14 at 16:25
  • 3
    @slm It isn't opinion-based, or at least it wasn't meant to be. One could require a lot more overhead, and unnecessarily use resources, especially in a more complex scenario. One could be conventionally more correct in the *nix world, or maybe there's even a formal standard somewhere. Programmers get annoyed over "sloppy coding" all the time, even when for all intents and purposes it achieves the same goal. – skittleys May 16 '14 at 17:43

One difference between the two is that aliases are only a shell feature. Environment variables are inherited by all subprocesses (unless deliberately cleared).

The environment variable would be more likely to work even if less is launched indirectly, such as via another shell (e.g. tcsh), man, vim, psql, etc.

  • 4
    In particular, aliases only work in interactive shells: you can’t use them in shell scripts, which you might sometimes want. bash -c 'alias ll="ls -l"; ll' won’t use the alias. – deltab May 16 '14 at 16:36
  • ah, I didn't know that! So if I have export LESS='-R' in my .zshrc, open a zsh session, and then switch to bash in that same session (with no export in .bashrc), the environment variable will still be set? I would have thought both would be lost.... – skittleys May 16 '14 at 17:47
  • @deltab ah, yes, I do know about that distinction, thanks to grep's colouring issues. Thank you for pointing it out. – skittleys May 16 '14 at 17:50

It greatly depends on the tool you are using. There will be tools that allows you to use either, others that only allows one.

There are commands that are called by others and read the environment variables and accept the same switches, but adding to each one the same switch is a hassle, like compilers as make, here environment variables shine. You set the variable and forget about it. It's also a plus that it can be temporal and work for a single command.

Bottom line, is a mater of preferences and the best action for the task, there will be times where setting up an alias is easier and permanent than an environment variable and other times where you need an array of tools behaving the same way where you would love environment variables.

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.