Unix/Linux Go Back    


OS X (Apple) OS X is a line of Unix-based graphical operating systems developed, marketed, and sold by Apple.

Don't understand the practical difference between command aliases and environmental variables

OS X (Apple)


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-09-2016   -   Original Discussion by scrutinizerix
scrutinizerix's Unix or Linux Image
scrutinizerix scrutinizerix is offline
Registered User
 
Join Date: Sep 2015
Last Activity: 17 March 2018, 12:38 PM EDT
Posts: 26
Thanks: 7
Thanked 1 Time in 1 Post
Don't understand the practical difference between command aliases and environmental variables

Hey,
I'm recently learning Unix from the video course by Kevin Scoglund. I'm stuck at the moment where he goes into Environmenat variables. I have some issues with understanding what's the essential difference between EV and command aliases: for instance, by writing the command



Code:
alias ll='ls -lahG'

into .bashrc file we ensure that typing every time the shortcut ll we get the whole bunch of modifications to the original command executed whenever we type it.

ALong with that we have so called "environmental variables" (f.e., export LESS='-N') we could equal to any value ("variable") that theoretically when saved to .bashrc should trigger that command exactly as we predefined it.
According to Mr. Scoglund it should work once we launch another Terminal session by opening a new window ir by executing

Code:
source .bashrc

. However, when trying the former it fails to do so (no command found), so I have to rely on the latter which is pain in the ass typing it in every time. It should be mentioned he worked with Snow Leopard while I'm on Lion, so definitely some changes occurred here.

So:

(1) I can't execute .bashrc file commands unless I execute

Code:
source .bashrc

Is there less labourous method of doing that without

Code:
source .bashrc

in Lion?

(2) what's the point of EV since one could assign an alias to a command and the result is pretty much the same?

If I sound dumb forgive me since I was forced to skip that entire chapter about profile, login and resource files due to corrupted video file containing that part Linux so I might have missed some points completely hence is my confusion.

Last edited by scrutinizerix; 01-10-2016 at 06:29 AM..
Sponsored Links
    #2  
Old Unix and Linux 01-09-2016   -   Original Discussion by scrutinizerix
vbe's Unix or Linux Image
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 22 June 2018, 3:50 AM EDT
Location: Switzerland - GE
Posts: 6,545
Thanks: 308
Thanked 563 Times in 525 Posts
If you put your alias in .bashrc, that is the file to be sourced like this


Code:
 . ./.bashrc

read dot space dot/....
The Following User Says Thank You to vbe For This Useful Post:
scrutinizerix (01-10-2016)
Sponsored Links
    #3  
Old Unix and Linux 01-09-2016   -   Original Discussion by scrutinizerix
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 22 June 2018, 4:40 AM EDT
Location: San Jose, CA, USA
Posts: 11,327
Thanks: 640
Thanked 3,945 Times in 3,372 Posts
When you are typing commands into a shell (such as bash), the shell expands aliases that you have defined (such as ll in your example). Those aliases are not expanded when your shell is executing commands contained in a file (i.e., a shell script).

Some utilities look at variables defined in their environment and if specific variables are found, those utilities behave differently. The environment variable affects they way those utility behave no matter how that utility was invoked (both by typing the utility name into the shell and by finding a request to run it from a shell script). When export LESS=-N is in the environment of your shell on OS X and you invoke the less utility, the output produced by less will include line numbers in the output.

For example, if you type in the commands:


Code:
unset LESS
man less|less

you will see the man page for the less utility. But, if you type in the command:


Code:
export LESS="-N"

and later type in the command:


Code:
man less|less

you will see the man page for the less utility with each line preceded by its line number.

I personally believe that the LESS environment variable (and similar environment variables designed to alter the behavior of a commonly used utilities) is dangerous because some shell scripts depend on normal less output and if they invoke less not knowing that LESS has been set, the output they see may be misinterpreted.

But there are some environment variables that are expected to be set to get proper operation from lots of utilities. These include, but are not limited to:
  • HOME which defines your home directory,
  • LANG and the LC_ALL, LC_COLLATE, LC_NUMERIC, .... which specify the codeset that is to be used when reading and writing files and what language and locale-specific customs should be used when looking at dates, times, numbers, sorting characters, etc.,
  • PATH which defines the directories your shell should search when trying to find commands,
  • TERM which tells utilities formatting output for your terminal lots of things about the capabilities of the terminal (or terminal emulator) you are using,
  • USER which defines the name of the user who logged into the current terminal session,
  • etc.
Run the env command to see what is currently set in your environment.

Note that if PAGER is set to less in your environment and you have set LESS in your environment to -N, the output from the man command will include line numbers (and it may make reading the man pages harder since they were formatted assuming that line numbers would not be included when they were displayed). Note that environment variable names (and utility names and option names and most other strings used in shell commands and scripts are case sensitive).
The Following 3 Users Say Thank You to Don Cragun For This Useful Post:
hicksd8 (01-09-2016), jim mcnamara (01-09-2016), scrutinizerix (01-10-2016)
    #4  
Old Unix and Linux 01-10-2016   -   Original Discussion by scrutinizerix
scrutinizerix's Unix or Linux Image
scrutinizerix scrutinizerix is offline
Registered User
 
Join Date: Sep 2015
Last Activity: 17 March 2018, 12:38 PM EDT
Posts: 26
Thanks: 7
Thanked 1 Time in 1 Post
Wow, thanks for such an extended and prompt reply, sir. Definitely my set of questions considering this not exhausted so will return here frequently.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Another Simple BASH command I don't understand. Help? phunkypants UNIX for Dummies Questions & Answers 3 03-10-2011 06:39 PM
[bash] command line substitution with environmental variables ASGR Shell Programming and Scripting 7 03-07-2010 09:14 PM
don't understand a command "." Moumou Shell Programming and Scripting 3 08-05-2009 08:53 AM
i don't understand the "sort" command xyyz UNIX for Dummies Questions & Answers 2 03-15-2002 11:16 AM



All times are GMT -4. The time now is 05:27 AM.