Clevers Rungene

Clevers Rungene

Learn Git and its Power

Learn Git and its Power

Shell Workshop

Clevers Rungene's photo
Clevers Rungene
·Aug 17, 2021·

9 min read

I recently learned how important and powerful Git is. I have decided to write this guide so that in case i forget i can always refer. I also hope it helps someone else.

What is Bash?

Bash is the shell, or command language interpreter, for the GNU operating system. While the GNU operating system provides other shells, including a version of csh, Bash is the default shell. Like other GNU software, Bash is quite portable.

What is a shell?

At its core, a shell is simply a macro processor that executes commands. A Unix shell is both a command interpreter and a programming language. Shells may be used interactively or non-interactively.

A shell allows execution of GNU commands, both synchronously and asynchronously.

While executing commands is essential, most of the power (and complexity) of shells is due to their embedded programming languages.

The Command Line

The Command Line Interface(CLI ) is a text interface that is navigated by typing commands at prompts, instead of using the mouse through the Graphic User Interface(GUI).

Why All Developers Should Learn Command Line

The ability to build larger programs from smaller ones is perhaps what gives the command line its power and flexibility. Read more

Intro to the shell

The shell is a command line interface for running programs on your computer. Developers mostly use it as it is fast and flexible. The program we use to interact with the shell is called a terminal program. If you're on Mac or Linux you already have one. If you're on windows you may need to install a shell and a terminal program that works the same as the one on Mac/Linux.

I assume you're using a Bash shell.

Windows: Installing Git Bash.

Download Git here

Opening a terminal

It doesn't matter which terminal program you end up using .The terminal is an interface to the Shell and other command line programs running inside it.

opening a terminal program.png

Something to keep in mind: You can have multiple terminals open at once. Each one of them has its own instance of the shell. A separate process on your computer.

Commands: Your first command (echo)

The echo: how we get the shell to print message back to us.

echo.PNG

NB: if you ever type something into the shell and it seems to be treating what your saying in a weird way, consider putting single quotes(‘!!’) around it.

Navigating directories (ls, cd, ..)

ls=List: List the files in the current directory.

ls.PNG

Listing another directory. Type ls followed by the name of that directory.

ls-2.PNG

cd=change directory: Move to a different directory i,e changing the directory the shell is looking at.

cd.PNG

cd .. also called the parent directory:if you want to go back a directory.

;lets you write two commands in the same line. The shell will run them in order.

parent-directory-ls.PNG

Current working directory (pwd)

pwd: Print Working Directory This is whatever directory the shell is currently looking at.

pwd.PNG

~: My home directory

Regardless of the directory your cd into ls ~ will list the home directory.

ls-tilda.PNG

Parameters and options (ls -l)

Some commands have parameters or options to help you get more information or change the default behavior of those commands.

Example

ls -l :prints a longer more detailed listing of files.

ls-l.PNG

ls -l Downloads/*pdf : lists all the pdf documents inside downloads directory.

ls-l-pdf docs.PNG

ls ~: lists files in my home directory.

ls bear* :The pattern matches any file whose name starts with word bear.

ls bear :will list any file whose name contains’ bear’.

Organizing your files (mkdir, mv)

mkdir: Creates directories.

mv: Move files from a directory to another.

Below we make a directory called Books in Documents then we move all pdf files from downloads to books.

organizing your files.PNG

NB: when you quote something in the shell, you always use straight quotes. This is what you'll get if you type into a terminal window. However, if you copy and paste from a web page or document, you should be really careful to make sure that it hasn't accidentally been written with “curly quotes”. Curly quotes will not work in the shell. Single quotes and double quotes do slightly different things in the shell. If you're unsure which to use, go for single quotes.

mkdir -p: create sub-directories of a directory, but only if they don't exist already.. It will create a parent directory first, if it doesn't exist. But if it already exists, then it will not print an error message and will move further to create sub-directories.

How to create a directory and switch to it using a single command?

$ mkdir [dir-name] && cd $_

$_ expands to the last argument to the previous simple command* or to the previous command if it had no arguments.

You can combine the above two commands e.g

mkdir -p my-git-course/new-git-project && cd $_ (Before running this command, make sure you cd to where you want these files stored. For example, if you want the files stored on your Desktop, then make sure you cd to the Desktop before running the command).

Downloading (curl)

curl: stands for C URL(see a webpage) is used in command lines or scripts to transfer data. Downloading files from the web.

culr -Lgoogle.com’ outputs compressed javascript to google.com homepage .This is equivalent to opening view source in a browser dev tools.

curl-downloads files from internet.PNG

To get the shell to write to a file instead of displaying to a terminal we use -o option:

curl -o google.html -L google.com’ creates and saves a file named google.html in our Downloads directory.

curl-download and save a file.PNG

curl -o dictionary.txt -Ltinyurl.com/zeyq9vc

The above command downloads and saves dictionary.txt

NB: -L means follow web redirects.

dictionary-txt.PNG

By the way, a lot of URLs have special characters in them, such as the & sign, which have unusual meanings to the shell. That's why I'm always putting these URLs in quotes … even though these particular examples would work without them, it's a good practice to get into.

Viewing files (cat, less)

cat: Short form for catenate or concatenate which means to run several things together- View file content.

cat dictionary.txt :ready through all the dictionary.txt

cat-dictionary.PNG

Please know this blasts the file out of the terminal so quickly that it goes right to the end. Thus it may not be very useful if you want to see the top of the file.

less: View file content screen by screen.you press space to move down. You can also use the arrow key. You press q to quit when you are done with less command.

less.PNG

less-screen-screen.PNG

You can use / to search

search through.PNG

search-results.PNG

Removing things (rm, rmdir)’

rm: remove.Delete files. Please know this command removes the file completely i.e the file does not got to recycler bin/trash can.

rm ‘H1 MINUTES.pdf’ removes a pdf file named H1 MINUTES from our downloads.

rm.PNG

If you want a warning before removing a file you can use -i (i is for interactive).

remove the files interactively.PNG

rmdir: Remove/Delete directories

remove-directory.PNG

Searching and pipes (grep, wc)

With the Shell, you can use commands to search content in files.

grep: "global regular expression print,” processes text line by line and prints any lines which match a specified pattern.

grep shell dictionary.txt :reads the file and outputs all the lines that contain that word(in this case ‘shell’).

grep.PNG

If there are more lines than we can see at once we can use the below command to ask the shell to send the output of grep into the less command.

grep shell dictionary.txt | less

grep-less.PNG

grep-less-output.PNG

grep can also operate from an input from another program. E.g you can pull a file from the web and immediately grep it for a particular pattern without having to save it to a file first.

curl -L tinyurl.com/zey9vc| grep anton

grep-anton.PNG

wc: "short for word count" reads either standard input or a list of files and generates one or more of the following statistics: newline count, word count, and byte count.

The below command counts how many matches are there (how many words are there that contain the string anton) .

curl -L tinyurl.com/zey9vc| grep anton | wc -l

grep-wc-l.PNG

Another way is to give grep an option -c for count.

curl -Ltinyurl.com/zey9vc | grep -c anton

grep-c.PNG

What are grep patterns called? Regular Expressions.

Shell and environment variables

The shell has variables :

shell-variables.PNG

There are two different kind of variables in the shell:

Shell Variable- this are internals to the shell program itself

shell-lines x columns.PNG

Environment variables-They are shared with the programs that you run from within the shell. E.g the $PATH variable. This tells your system where your program files are,when you type a command it can find a program to run it.

echo-path.PNG

Startup files (.bash_profile)

The shell is not just a user interface it is also a programming language. Files containing shell commands are called shell scripts To make changes sticky in the shell you have to put them in the shell's configuration files.

On Windows and Mac the bash shell runs .bash_profile to get its configuration. In Linux it runs .bashrc . Any commands you put on this file will be run every time you start the shell. E.g maybe you want to see a date and a friendly message.

bash_profile.PNG

When you start a new shell the commands you put there get run.

bash_profile_executed.PNG

For users on Windows OS and using Git Bash you won't have a .bash_profile file.

This is how you create it.

1) If you're not in the home directory, change into it.

2) Create a file using touch .bashrc

3) Then edit it with Vim vim .bashrc

4) Change into insert model by hitting i

5) Exit the insert mode by hitting esc

6) Save and close the file typing :wq and hit Enter

7) Restart your Git Bash shell.

8) When you open your Git bash it will create a .bash_profile for you.

9) To open .bash_profile form your shell types vim .bashrc. Edit the file ,save and close the file as explained above. Restart the shell to see the changes.

Additional Resources

For a deeper understanding, you can read this post

Aliases

alias: Allows you to create short names for commands.

This is another cool trick for customising the shell .It is a way of making long shell commands shorter. e.g alias ll=’ls -la’

ll is a short name to a longer command ‘ls -la

Just as you are assigning variables the alias command does not want to see spaces on either side of the equal sign. Otherwise it will give you a bunch of errors.

After you create an alias any time you type a short command e.g ll the shell will just run the long command.

alias.PNG

If you want to list all aliases you have just run alias command without arguments.

all-alias.PNG

You can use aliases the same way as regular commands e.g ll Downloads is the same as: ls -la Downloads

ll-directory.PNG

Keep learning!

Shell resources

The Bash Academy

Bash Beginners Guide

Bash Programming HOWTO

Regexr — Learn Regular Expressions

 
Share this