DEV Community

Cover image for [2/4] Beginners BASH basics - learning the way of the shell
Hayden Rouille
Hayden Rouille

Posted on • Edited on

[2/4] Beginners BASH basics - learning the way of the shell

Part 2 of a 4 part series

Part 1 is available here and part 3 here

In part 1 we discussed commands that will give you your first feel into the world of the command line. We learnt how to read the docs, look at and read your files, remove them and even search in them, but now it's time to learn some tools that will really increase your productivity in the terminal.

Part 2 aims to give you the knowledge you'll need to start using the command line over your editor in certain scenarios. You'll learn how to start reading what's actually happening in some basic scripts and more importantly, you'll be able to start being super fast and productive inside the often daunting black and white world that is the terminal.

superfastwhaaaaaa

Let's dive straight in.

touch

touch will create a file, in the same way that mkdir will create a directory. You will learn later on how you can save a file directly through the vim editor, however there may be an occasion where you want to create one or multiple files but not edit them at the time.

Example usage: touch projects/TODO.md

echo

echo is a versatile command that allows you to print arguments or variables. echo "Hello, world!" to print out some plain text to the terminal, or echo $HOME to view an environment variables value. (P.S, we'll get more into that on part 4)

Example usage: echo can be used just print a value, to append a line to the bottom of a file with echo $HOME >> my_file.md or even to override a files content from the top with echo $HOME > my_file.md.

curl

curl is used to transfer data from or to a server. You can for instance try curl https://dev.to which will give you the response for here!

Example usage:
You may see the following common usage for curl, which will place the output to the file specified (setup.sh): curl -fLo ~/setup.sh https://raw.githubusercontent.com/haydenrou/dotfiles/master/setup.sh

ping

ping will send packets to a host, expecting the host to receive them. It's often used to check that a host is up and running.

Example usage: ping dev.to

Tip: Recently I came across an issue on one of my servers where I couldn't connect to github. My first point of call was to ping github.com, to see if the issue was there. I knew my server had internet access, but for some reason my ping was dropping off and not working. This was preventing me deploying my application and in general was just a pain.

To fix it, I simply added 140.82.112.3 github.com www.github.com to my /etc/hosts file by using echo "140.82.112.3 github.com www.github.com" >> /etc/hosts which makes sure when trying to access github, my server knew where to go to. Problem solved.

chmod

chmod can be used to change file modes.

One of the most common usage is to change a file to be executable, for example if you've created a script and want to run it.

Example usage: chmod +x my_script.sh

And then you can run the script like so sh my_script.sh or ./my_script.sh

You can use chmod to change permissions to be read only, read and write, or just about whatever you want - you can see all the uses in the man page!

less

When you open up a man page or use some other utilities, you may notice that the way the file is shown is within a menu that at first may be unfamiliar.
You may gather quickly that you can go up and down using the arrow keys or hjkl if you use vim, and that you can exit using q, but I always wondered for a while... what exactly was this?

In most cases, this opens an instance of less. less will open a file from the top, and allow you to scroll through it as mentioned above, or search for what you're looking for. less is great if you want to quickly skim a file or see the contents in an easier to navigate way.

Example usage: less my_file.md

Tips: ctrl-d and ctrl-u will go down and up respectively. /my_search will search when you press enter and bring what you're looking for to the top. And of course you can go up and down slower using hjkl or the arrow keys.

tail

As you've probably guessed, tail will show you the tail end of a file.

Why is this useful? Well you can view the constant updates to a file by using tails -f flag, and see it updating on the spot.

If you have a file (like a Rails application's log for example) that you want to follow, you can run tail -f log/development.log (or production.log in the relevant environment) to see the stream of output.

Example usage: tail -f log/development.log to show a constant stream, which you can end by pressing ctrl-c. tail -n 5 log/development.log to view the last 5 lines.

conclusion

That's part 2 of our beginners BASH basics, I hope you enjoyed it! Next we're going to dive deep into some more complex tools and programs such as vim, sed and awk, so stay tuned!

As always, feel free to ask any questions or if you're interested in how I've customised my environment, head over to my repository below.

GitHub logo haydenrou / dotfiles

My Personalised Development Environment

Check out part part 3 here!

Top comments (2)

Collapse
 
ryboflavin42 profile image
Ry

Never echo logout >> ~/.bash_profile

Collapse
 
hayden profile image
Hayden Rouille

true!