Getting started with bash
Tags: bash
Published: 2019-06-24 13:10:50

---
title: Getting started with bash
published: true
description: Let's get into this thing!
tags: bash
---
# Introduction

What the hell is bash anyway? Before doing any kind of research, I actually had a very difficult coming up with a simple explanation.

Bash is a _program_ on your computer. This program is designed to take commands from you, the user. But bash is programmed to do a myriad of tasks. To make sure this program is efficient, a bash _language_ has been created. This language allows you to speak to the bash program to tell it what to do.

_bash_ stands for _Bourne Again SHell_ and it is the default shell in most of the Linux distributions and OS X. There are different kind of shells, C shell (csh), Z shell (zsh), Korn shell (ksh)... Because I'm on OS X and bash is the default shell on my machine, I'll be using this.

## How does it take commands?

Bash has two ways to takes commands:

- Waits for the user to type commands in a command line interface ( usually your terminal application). This is called _interactive mode_.
- Interprets a text file that contains the commands you want to execute. This is called _batch mode_.

So, you won't have any GUI ( Graphical User Interface ) to interact with bash. It takes text commands, its simplicity is its power.

## What does it do?

SO MUCH... I've only scratched the surface and I'm already amazed... Editing files and images, then converting them. Moving and copying files, create back-ups. Download code, compile the code, run the code...

Bash is an amazing tool. By learning how to use even some simple commands ( and you already do :wink: ), you will make your life a lot easier.

## Less talk, more bash?

If you are on a Linux or OS X, you most likely already have bash installed. On Windows 10, I do believe you can active something called _Windows Subsystem for Linux_.

Open a terminal. I'm using iTerm2, but any terminal will do the trick. Let's type our first command:

```bash
$ echo $BASH_VERSION
5.0.7(1)-release
```

First bash command completed! You successfully just returned the version of bash you are using.

_Note:_ The terminal is not bash. The terminal is what's making the text appear on your screen, inside the interface. Bash is one of the many programs that can run on your terminal.

![](https://thepracticaldev.s3.amazonaws.com/i/litkchpd9keql5s486fv.png)

Ok, let's write our first bash script. Create a file called `script.sh`.

Every bash script will start with a _shebang_ that specifies the full path of the command interpreter that will be used to run the script. Mine will be:

`#! /usr/local/bin/bash`

They all start with _#!_ followed by the path. To get your path, you can run `which bash` in your terminal.

Next, let's do some simple commands:

```bash
#! /usr/local/bin/bash

echo "Hello World!"

echo "What is your name?"

read name

echo "Nice to meet you $name!"
```

To make sure our computer doesn't explode, we must respect the ancient traditions of our people and start with a _Hello World!_. Once this is done, we use the _echo_ commands to print some strings. We use the _read_ command to ask the user for an input. We store the input in a variable called _name_ and print it out on the terminal.

Starting niiiiiice and easy.

Now, we need to execute this script. But, for security reasons, scripts are not executable by default. To change this, you must run:

`chmod +x script.sh`

Great, now run the script:

`./script.sh`

```bash
macbook-air-de-damien:bash Damien$ ./script.sh
Hello World!
What is your name?
Damien
Nice to meet you Damien!
macbook-air-de-damien:bash Damien$
```

The variable _name_, which I populated with _Damien_ is properly printed back on my terminal.

Let's edit our script to do something different and exploring other commands.

```bash
#! /usr/local/bin/bash

printf "Hello `whoami`!\n"
printf "Today's date is $(date)\n"
printf "You are here => $(pwd)\n"
read -p "Give me the answer to the universe: " answer
if [ $answer = "42" ]
then
	echo "Damn right it is!"
else
	echo "So wrong!"
fi
```

OK, we have a few things here.

- The first three lines prints the results of different commands (_whoami_, _date_ and _pwd_). Notice than you can use two syntaxes, `whoami` or \$(whoami). _whoami_ gives you the current username, _date_ gives you the date and _pwd_ gives you the directory you're in.

- We also use another command to print to the terminal called _printf_. This command doesn't come with an automatic newline character like _echo_. This is why we had the _/n_ character.

- Then, we have another _read_ command. This time, we added the _-p_ flag to create a prompt. Instead of using a combination of _echo_ then _read_, we combine them into one line!

- Finally, we have a if/else statement. We put our if condition inside brackets [].

**YOU HAVE TO PUT SPACES AROUND THE CONDITION**.

Failure to do that will result in an error. You've been warned!

So, if the answer variable is equal to 42, we print something, else we print something else. The *fi* keyword indicates that the if stops right there. Forgetting this keyword will also give you an error.

## Conclusion

Ok, I feel like this is enough for a first article on bash. We have seen what bash is, how to interact with it and used some simple commands. Next time, we'll dive deeper :wink:

Happ coding :heart: