Bash your unproductive time! Or how to set up aliases.
The more time I spend working on different data science projects, the more I spend navigating through my project using the command line. In this post, I’ll share some tricks and bash shortcuts I have picked up along the way. If you don’t know what bash means, you are in the right place! Just keep reading.
Before we dive into how I have my environment set up, let’s review some concepts that will help you set up your own computer, especially if you are new to navigating through the terminal.
What is a Shell?
Simply put, the shell is the software you use to interact with the operating system. These interactions include file management (copying/moving/deleting files or folders), repository management (cloning, pulling or pushing your commits), process management (running or terminating applications, for example starting Jupyter Notebook), monitoring and configuring your operating system (for example installing of updating your software). But wait! There is more! The shell is also its own programing language! You can create variables, functions, loops, if-else conditionals, etc.
There are text-based, known as a command-line interface (CLI) as well as graphical user interfaces (GUI).
There are different shell flavors, the most popular ones are: bash (Bourne Again shell), csh (C shell), zsh (Z shell), tcsh (T C shell), ksh (KornShell). If you are running in a Mac or a Unix/Linux OS, you probably have these installed already. If you are a Windows users have the DOS shell, or Powershell for Unix-type commands. Each shell comes with different features and different command syntax, try them out!
Once you select a shell, you may find a whole new world of frameworks, themes, and plugins to choose from. In this post, I just focus on bash.
Not sure what shell you are running? Keep reading :)
The terminal
The terminal is the application you will use to run the shell commands. As with shells, there are multiple terminal applications to choose from, for OS X there is Terminal, iTerm2, Hyper, Alacritty; for Windows systems take a look at PowerShell or Cmder.
To find out which shell you are running, start the Terminal app, and type the command
echo $0
To see which shells are available, type
cat /etc/shells
To change the default shell, open the terminal preferences and select your preferred path under “Shells open with”.
Once you’ve selected your shell, (bash for this post) create “.bash_profile” file into your home directory. Note the dot at the beginning of the filename, this means it will be hidden. In this file, you will write all the shortcuts you want. The next time you start a terminal, the shell will use all the alias and functions declared here.
ALIAS!
An alias is a shortcut to reference a command.
Why use aliases?
- Specify the default options for a command
alias l='ls -ltah'
- Avoid typing long commands, and improve efficiency. For example to print a full recursive list
alias lr=’ls -R | grep “:$” | sed -e ‘\’’s/:$//’\’’ -e ‘\’’s/[^-][^\/]*\// — /g’\’’ -e ‘\’’s/^/ /’\’’ -e ‘\’’s/-/|/’\’’ | less’
- Specify a default version of a program
alias py='python3.6'
- Avoid common misspellings of command
alias pdw='pwd'
Check out my bash profile at the end of the post.
Changing your terminal prompt
Through the .bash_profile you can also customize the terminal prompt to show some colors or to show some relevant information about the folder you are in. For example in my .bash_profile, I only show the username (in black), the current folder (in blue), and the name of Git branch. The name of the Git branch is red if changes need to be committed, yellow if the local branch is ahead, and green if working directory if up to date.
WARNING!: Be careful with the names you use for your aliases. The alias will overwrite any previous commands. Make sure you select a name that is not a bach command or used by any other program!
FIN