DEV Community

Cover image for [4/4] Beginners BASH basics - The student becomes the master
Hayden Rouille
Hayden Rouille

Posted on

[4/4] Beginners BASH basics - The student becomes the master

Part 4 of a 4 part series

Part 1 is available here, 2 here and 3 here

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

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

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

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

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.

I found the .bashrc file and I want to know the purpose/function of it. Also how and when is it used?

To source your rc files, you can use the source keyword, or ..

source ~/.bashrc
. ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

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 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

A simple function could look like the following:

function say_hello () {
  echo "Hello, world!"
}
Enter fullscreen mode Exit fullscreen mode

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 logo haydenrou / dotfiles

My Personalised Development Environment

haydenrou/dotfiles

<strong>My Personalized Development Environment</strong>
Enter fullscreen mode Exit fullscreen mode


Dotfiles Demo

To prevent myself from going down wild Linux rabbit holes and trying every distribution I can get my hands on, I've transcended into an Apple chad. Therefore, this configuration may require changes to work in a Linux environment.

Mandatory Software:

Optional Software: (Warning: you may have to remove some alises / plugins if these are not installed)





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!

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 :)

Top comments (0)