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.

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.

share|improve this question
1  
No it really comes down to personal preference. I voted to close this since it seems more opinion based. –  slm May 16 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 at 16:25
1  
@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 at 17:43

2 Answers 2

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.

share|improve this answer
    
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 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 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 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.

share|improve this answer

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.