What's the difference between storing aliases and environment variables?

Question

What’s the difference between storing aliases and environment variables?

Answer

Aliases are simply a new way that we give to commands so we can use them on our own terms, for example, let’s say that we would like to always start from the desktop and check location and see a list of files, but to do so we forcefully need to write cd ~/Desktop; pwd; ls every time. but with the alias we can simply say:

alias 2strt = cd ~/Desktop; pwd; ls

Now, after it is sourced we just need to run 2strt and it will direct us to Desktop, let us know that we are there and show the files and directories there.

On the other hand, environment variables are just like variables in programming languages, simply keywords that store data, in this case, usually sensitive data that we may want to keep offline.

Let’s say I have a route that I don’t want people to see me type if they are passing by, so I will create an environment variable for it in .bash_profile:

export MY_ROUTE='~/Documents/thisDir/otherDir/oneMoreDir/mySensitiveDir'

And so, after sourcing, I could say cd $MY_ROUTE and I will be taken to the location I need to work at.

So now we might find a little clearer where the differences are between aliases and environment variables, yes, they are both like keywords, but an alias holds a reference to a command and an environment variable just withholds data.

26 Likes

Does the

 $ 

really necessary when excuse the command

cd $MY_ROUTE

?

i’m confused. if its a secret, wouldn’t they see me type $MY_ROUTE?

they will see you type $MY_ROUTE, but they will not know where your “route” takes you, i.e. “~/Documents/thisDir/otherDir/oneMoreDir/mySensitiveDir”

If I create an alias, do I have to avoid spaces between “2strt”, “=” and “cd…”?

I tried it on my Mac and it seems that if I add spaces in between the above mentioned elements the terminal interprets it as if I want to create multiple aliases…

1 Like

There must no be any space between the new alias and the “=” and the string containing the comand, it should always go this way:

alias new_alias="my command"

You can see this article for more information: https://www.hostingadvice.com/how-to/set-command-aliases-linuxubuntudebian/