[4/4] Beginners BASH basics - The student becomes the master
Tags: beginners, bash, webdev, productivity
Published: 2020-07-14 17:04:03

## Part 4 of a 4 part series
Part 1 is available [here](https://dev.to/haydenrou/1-4-beginners-bash-basics-becoming-one-with-the-shell-mpk), 2 [here](https://dev.to/hayden/2-4-beginners-bash-basics-learning-the-way-of-the-shell-3f7b) and 3 [here](https://dev.to/hayden/3-4-beginners-bash-basics-wait-bash-can-do-that-4oh9/)

Now that you've learnt how to wield the command line to your advantage, I wanted to share some utilities that help with day-to-day tasks and show you how you can customise BASH to act how you want it to.

The power of BASH truly surpasses the extent of these articles, but when you grasp what is capable, the sky is your limit and you can take yourself in a new direction and start teaching me!

# scp
As you remember using SSH in part 3, I wanted to show you a quick tool that may solve a problem you ran into not long after using SSH. How do I move files to my server?
After developing an application, be it a static single page app or a fully fledged piece of software, you will reach the point where you need to move the files that you were likely developing on your local computer over to the server.
There are many tools to do this (such as capistrano for ruby), and I'm sure this is not the route you'd go down for each individual deployment, but nonetheless you will occasionally need to move secret keys or small files to your server.

Though we didn't cover it, `cp` is a command that will copy a file. `cp my_file another_directory/` will copy `my_file` to `another_directory`. Simple. 

![copy cat](https://media2.giphy.com/media/oCCLHVNt8YO64/giphy.gif)

`scp` though, takes it to another level. The secure copy can copy files over SSH using a secure copy protocol.

Example usage:
`scp ~/projects/my_stuff/my_file.md user@server:/home/user/app/`
If you have setup your SSH config file, you won't need to use the whole `user@server` as we learnt last time.

# htop
`top` is a utility that shows real-time information about running processes. If you want to see how much memory a specific process is taking up, or how hard your computer is working - use `top`. (or `htop`)

`htop` is an enhanced version of top, there are a few of them with the word `top` in the name, but they are all much of a muchness. I find `htop` to have some nice colors and it's easy to read. Check it out: `htop`
![htop](https://dev-to-uploads.s3.amazonaws.com/i/onwuq2l24g063jtu846f.png)

# ln
One important command I wanted to briefly touch on is `ln`. `ln` will create links between different files. For example, if you had a repository with your dotfiles in, you could link them to the location that your system expects them to be in.

Without the `-s` flag, `ln` will create a hardlink, and with the `-s` flag, a symbolic link. The difference is that a hardlink will link directly to the inode the target is stored at, and a symlink will link to a file or directory. You can read more [here](https://bencane.com/2013/10/10/symlinks-vs-hardlinks-and-how-to-create-them/)

Example usage:
`ln -s ~/projects/dotfiles/bashrc.symlink ~/.bashrc`

# oh my zsh

Whilst I don't use `zsh`, I thought it's worth touching on as it seems to be an industry favourite. `zsh` is an interactive shell built on top of BASH which utilises all the great parts of BASH and has some extras on top. It has great coloring and command prompt customizability, and if you use the `oh my zsh` framework, has some brilliant plugins you can install very easily to populate the most common aliases for tools such as `git`. 

Check out `oh my zsh` [here](https://ohmyz.sh/)

# customization

BASH (and `zsh` if you're using it) both allow you to customise your setup. This is done through the `~/.bashrc` file. (or `~/.zshrc`)

The `rc` files are loaded at the start of each session, so if you make a change, you'll have to `source` the file to gain access to your changes in the current session. If you want to read more about how sourcing the `bashrc` and `bash_profile` works, read the below.
{% stackexchange 129143 unix %}

To source your `rc` files, you can use the `source` keyword, or `.`.
```
source ~/.bashrc
. ~/.bashrc
```

Your `bashrc` can store all sorts of functions, variables or aliases that you want, here are just a few examples:

## environment variables
An environment variable can be stored by adding `export MYENVVARIABLE="my stuff"` to your `bashrc`. This can be useful because certain tools will check for environment variables to see where to store cache or load files. You can view your environment variable by using `echo $MYENVVARIABLE`.

## aliases
If you have a command you run frequently, you can alias it to something smaller to make it easier. Add an alias to your `bashrc` like so: `alias gd="git diff"`, and then access it after sourcing your `bashrc` by just typing `gd`.

## functions
If you have a set of logic that you want to put into a single command, you can create a function in your `bashrc`. I use [feh](https://github.com/derf/feh) which can change my backgrounds via the command line, and so I created an alias of a function I made called `chbg`, which changes my background to a random one from a folder. Check out an example [here](https://github.com/haydenrou/dotfiles/blob/84c31ca61e1b83fa4bf75b53c36966726791c37c/bashrc.symlink#L85)

A simple function could look like the following:
```
function say_hello () {
  echo "Hello, world!"
}
```

# conclusion & extras
Now that you've learnt more than the basics of BASH, you can explore the world of scripting and play around with your own setup until your hearts content!

You can check out how I've used the above techniques to make my workflow easier in my configuration repository below. You can see most of the useful parts in the README but have a look through the files to see how it's done! And don't forget to give it a star!

{% github https://github.com/haydenrou/dotfiles %}

I've left out my 2 favourite command line tools from these articles, and made an article dedicated to them, check out how to optimize your workflow with fzf and rg below!

{% link https://dev.to/hayden/optimizing-your-workflow-with-fzf-ripgrep-2eai %}

Let me know in the comments if you have learnt of some other cool tools or if you've written something thats improved your workflow - I'd love to hear about it!

And that's a wrap - I hope everyone has enjoyed the series :)