.bashrc

From Thom Group Wiki
Jump to navigation Jump to search

Upon startup the .bashrc file is executed. It contains command line inputs. Add new inputs to this file if you find yourself wanting to issue the same shell commands over and over, such as modifying the $PATH. You can also create alias, and short functions.

Nature of the beast

The .bashrc file is hidden, and lives in the home (~) directory of the user. To see hidden files in a list, use ls -a. If you have made changes to .bashrc, you can rerun the file while in the home directory rather than restarting the terminal.

". .bashrc"

Path

The path statement is where the shell checks for executable files. These files must be in the path, else they will not be found. Your path always includes the directory you are currently browsing, but you can add other directories to be permanently in the path by writing a line of the form:

export PATH=~/qcmagic:$PATH

Python also uses a path statement to execute its files. The syntax is exactly similar:

export PYTHONPATH=~/qcmagic:$PYTHONPATH:


Alias

Sometimes you will find yourself entering the same keypresses over and over again. An alias replaces a certain word, whenever you input it, with another series of key presses. You can use this facility to reduce long, commonly-encountered combinations or addresses to a single word. You can also alias common typos to their correct input, so that even if you misspell 'mkdir' as 'mdkri' or 'mdkir' it still works!

work="~/qchem/MnO4/scratch/manout"
ll="ls -lrt -a"
mdkir="mkdir"


User Function

An alias is not always enough. We can go further and define a word to execute an entire series of commands. Even take inputs or arguments!

A function to remove all files and directories below the current one: ded () {

   printf "\033[0;32mYou Are Here:\033[0m\n"
   ls
   printf "Are you \033[0;31msure\033[0m you want to do this?\n"
   read yon
   if [ $yon = yes ]; then
       printf "\033[0;31mFire in the hole!\033[0m\n"
       rm -rf *
   else
       printf "\033[0;32mStand Down\033[0m\n"
   fi

}

A function to print the directory contents whenever you change directory: function cd {

   builtin cd "$@" && ls

}

A function to perform the grep command and then edit the output: grepl () {

   grep energy $1 | awk '{print $4,$3}' | sort -n | head -1

}