syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

You are here: Linux101 > Shell 101 > shell101_vi_bash_aliases

Linux Shell 101

Question:
In Shell101 why .bash_aliases ?

Earlier, in Shell101, we learned some vi commands while editing ~ann/.bashrc

That exercise was our first demo of shell programming.

Next, I write a file used by the Bash shell called ~ann/.bash_aliases

I can see the above file when I do this:
ann@feb ~ $ 
ann@feb ~ $ cat .bashrc
# .bashrc for ann

export PS1="`id -un`@`hostname` \w $ "

# If I have ~/.bash_aliases ]
# I should dot it.
if [ -f ~/.bash_aliases ]; then
    .   ~/.bash_aliases
fi

# If I have some bin folders,
# I should add them to my PATH env variable:

if [ -e        ~/bin ]; then
  export PATH="~/bin:$PATH"
fi

if [ -e        ${HOME}/anaconda/bin ]; then
  export PATH="${HOME}/anaconda/bin:$PATH"
fi

if [ -e        ${HOME}/anaconda3/bin ]; then
  export PATH="${HOME}/anaconda3/bin:$PATH"
fi

ann@feb ~ $ 
ann@feb ~ $ 
ann@feb ~ $ 
The syntax around .bash_aliases is a good example of this English logic:

If ~/.bash_aliases exists then
dot ~/.bash_aliases

In Bash, what does it mean to 'dot' a file?

Answer: When I 'dot' a file, I run it. Also any values of env variables touched by the file are retained.

If I run a file instead of dot-ing it, will I retain values of env variables touched by the file?

Answer: No!
So, if I need to use a script or file to change env variables, I need to 'dot' the script.

What is an env variable?

All languages have variables. Bash variables are called env variables. The first env variable you encounter might be PATH. If you want to see the value of PATH type this:
echo $PATH
Now I want to create the second shell programming demo of shell101.

I can use my favorite editor to type some Bash syntax into the file: ~ann/.bash_aliases
# ~ann/.bash_aliases

# This file should contain Bash aliases.

alias ll='ls -la'
Next, I can 'dot' the file:
ann@feb ~ $ 
ann@feb ~ $ 
ann@feb ~ $ pwd
/home/ann
ann@feb ~ $ 
ann@feb ~ $ cat .bash_aliases 
# ~ann/.bash_aliases

# This file should contain Bash aliases.

alias ll='ls -la'

ann@feb ~ $ 
ann@feb ~ $ . .bash_aliases 
ann@feb ~ $ 
ann@feb ~ $ 
Then I can test the alias:
ann@feb ~ $ 
ann@feb ~ $ 
ann@feb ~ $ alias
alias ll='ls -la'
ann@feb ~ $ 
ann@feb ~ $ ll
total 346816
drwxr-xr-x  9 ann  ann       4096 Feb 20 00:44 .
drwxr-xr-x  6 root root      4096 Feb 18 22:32 ..
drwxrwxr-x 15 ann  ann       4096 Feb 19 11:15 anaconda
-rw-rw-r--  1 ann  ann  353806962 Feb 19 11:10 Anaconda-2.1.0-Linux-x86_64.sh
-rw-rw-r--  1 ann  ann         84 Feb 20 00:40 .bash_aliases
-rw-r--r--  1 ann  ann        220 Apr  9  2014 .bash_logout
-rw-rw-r--  1 ann  ann        489 Feb 19 00:23 .bashrc
-rw-r--r--  1 ann  ann       3637 Apr  9  2014 .bashrc.bak
drwx------  3 ann  ann       4096 Feb 19 11:17 .cache
drwxrwxr-x  3 ann  ann       4096 Feb 19 11:17 .config
drwxrwxr-x  2 ann  ann       4096 Feb 19 11:14 .continuum
drwx------  3 ann  ann       4096 Feb 19 10:53 .dbus
-rw-rw-r--  1 ann  ann        246 Feb 19 10:56 .emacs
-rw-rw-r--  1 ann  ann        210 Feb 19 10:55 .emacs~
drwx------  3 ann  ann       4096 Feb 19 10:54 .emacs.d
-rw-rw-r--  1 ann  ann    1180690 Feb 19 10:57 eur_usd_00.csv
-rw-rw-r--  1 ann  ann      52844 Feb 19 21:49 eur_usd1000_00.png
-rw-r--r--  1 ann  ann       8980 Oct  4  2013 examples.desktop
drwx------  2 ann  ann       4096 Feb 19 10:53 .gconf
-rw-rw-r--  1 ann  ann       6035 Feb 19 21:40 knn4ws.py
-rw-rw-r--  1 ann  ann          3 Feb 19 10:58 knn4ws.py~
-rw-r--r--  1 ann  ann        675 Apr  9  2014 .profile
-rw-------  1 ann  ann         49 Feb 19 10:53 .Xauthority
ann@feb ~ $ 
ann@feb ~ $ 
ann@feb ~ $ 
So, that is the second shell programming demo of shell101.



You are here: Linux101 > Shell 101 > shell101_vi_bash_aliases
You can ask questions in Dan's Machine Learning Class Forum:
https://groups.google.com/forum/#!forum/dan101

syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me