Knowing Bash is a very useful because it’s available on any Unix system. And when you are dealing with servers you have to use it a lot. Bash scripts can be as simple as a list of commands or be complicated with lots of functions and logic. Anything you can do in a terminal you can do in a Bash script and vice versa. This will be a very basic intro. It will show how to declare and use variables, make and use functions, and use if-else statements. But we’re gonna start with the hello world bash script.

Hello World

You can make a script and run it with one command if you want.

$ echo "echo hello world" > helloworld && sh helloworld
hello world

Above we are putting the output of echo "echo hello world" into a file called helloworld, and then (if that is successful) we run the sh program with our file as an argument. Now this is the laziest way to do it. We should at least call our file helloworld.sh so we know it’s a bash script. But better practice would be also including the interpreter at the top and making the script executable. Then we can run it by itself.

$ vim helloworld.sh

#!/bin/bash

echo hello world

$ chmod +x helloworld.sh
$ ./helloworld.sh
hello world

running a script with sh will work regardless of permissions. To run the script directly then you will need to make it executable. (I’ve had weird errors with one way and not the other so if that happens just use whichever way works)

Variables

Variable declarations are pretty simple. Type the name of the variable then the equal sign then what the variable will be. When you use the variable you precede it with a dollar sign.

foo="bar"
echo $foo

The biggest mistake people make when declaring variables is that they put space around the equal sign. There must be no space.

Functions

foo() {
  echo "bar"
}

The code of above declares a function. Even though it looks like a function you’d see in other languages like javascript it doesn’t behave that way. It has parenthesis, but no parameters will go there, and you don’t call a function with parenthesis either. You can call the function simply by typing the name. This is how it would work with arguments.

#foobar.sh

function foo () {
  echo $1
}

foo bar

$ sh foobar.sh
bar

if-else Statements

if [[ 1 == 1 ]]; then
  echo "one equals one!"
else
  echo "somehow one does not equal one"
fi

And alternate way to write an if-else is:

if [[ 1 == 1 ]]
then
  echo "one equals one!"
else
  echo "somehow one does not equal one"
fi

I prefer the former.

Arithmetic

Any variable that involves arithmetic must be preceded with let.
Screen Shot 2017-07-20 at 3.37.02 PM
That’s it for now. I’ll do another post on dates in Bash later.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s