Bash Profile

There is a really handy way to make your time in bash in the terminal a little bit easier with some shortcuts. Anything you repeatedly have to type into the terminal can be made into a shortcut, and anything that saves time is generally a good idea!

Let's add a shortcut that would allow me to get to my DSI6 lessons folder from any other folder in my machine. For these to be useful in every folder, we will need to use the absolute path to the folder rather than the relative path.

My Lessons folder is at /Users/Tim/Documents/DSI6/lessons so if I wanted to get there from any folder in my machine, I'd need to type: cd /Users/Tim/Documents/DSI6/lessons but I am lazy, so I don't want to do that every time, I'll make a shortcut so I can type 'dsi6' and immediately get to that folder

To find & edit your bash profile

type cd into the teminal. This will take you directly to your user folder

to check if you have a bash profile already set up, type ls -a and look to see if you have a file named .bash_profile (no worries if you don't). When you installed Anaconda, it probably made one for you anyway.

to edit this file type vim .bash_profile (this will create the file if it didn't exist before)

Within this file is where we will save some shortcuts

  • to get into insert mode, type 'i'

  • To add a commented line start the line with a '#' eg # DSI SHORTCUTS

  • To make the shortcut type: alias dsi6="cd /Users/Tim/Documents/DSI6/lessons" Remember that this is my path - not yours, so you'd need to replace the '/Users/Tim/Documents/DSI6/lessons' with the absolute path to your lessons directory

Once you have finished in vim, you'll need to hit escape, then type :wq (for Write, Quit)

We have just one more step. Every time you make a change in the bash profile, you'll need to reload the profile which you could do by either closing terminal (killing every running terminal session) and restarting or by typing source ~/.bash_profile in the terminal

An example block from my profile

alias gsu="git status -uno"
alias ga="git add"
alias gc="git commit -m "
alias gps="git push"
alias gpl="git pull"
alias gplu="git pull upstream master" 

Environment Variables

You can also set a variable in the profile that you can access in any commands

export EC2="ec2-18-191-81-235.us-east-2.compute.amazonaws.com"

I can then access this variable by using $EC2 in any command

More Useful Terminal Commands

Chaining commands

You can chain commands together within the shortcuts which is even better - less typing:

alias morning="cd /Users/Tim/Documents/DSI6/lessons && git pull upstream master"

this would allow me to type 'morning' into my terminal and immediately move to the lessons folder and pull today's lesson from the upstream repo

you can chain commands with either ';' or '&&'. if you use ';' the commands will all run regardless of any failure in previous commands wheras '&&' will only perform the next commands if the previous command did not cause an error.

Daemon Processes

You can run a command in the background by starting a daemon process. This is done by adding a space then & at the end of your command this will mean the process will run in the background even if you close the terminal window.

Find which Processes are running

You can see what processes are running on your machine by typing ps -ax you'll see that you get a huge list returned which is not super useful, so you can filter this output with grep. ps -ax | grep jupyter will show only the services running which have jupyter in the name.

Screen-Shot-2018-06-28-at-14.05.18

The pipe | character allows you to pass the output from one command to the next, in this case we are passing all the text output from ps -ax to grep, which will filter only the rows that contain 'jupyter'

Killing Processes

You can kill a process that is running by running the command kill followed by the PID. The PID is the number that is the first number on the row alongside the service you have found using the ps -ax command