Bash Scripting - devinziegler/Devin-Tech-Journal GitHub Wiki

Assignment: Bash Scripting

Bash or Bourne Again Shell is the default shell for most *nix operating systems. Because of the nature of Bash, automation and scripting is capable of doing just about anything.

Scripting Basics

Format:

#!/bin/bash

This file starter is known as shabang. The shabang allows the file to be executed as a program. However permissions may be needed to allow a user to execute.

chmod +x <scriptname.sh>

This command will change file permissions to executable for the current user. To execute use the following command:

./<scriptname.sh>

The ./ flag specifies that the file is to be executed. This is necessary when calling the script, although the executable can be run a different way using bash <scriptname.sh>

Piping:

Piping is incredibly useful for filtering out anything that is not immediately needed.

Basic Pipe Usage:

<command to filter> | grep <pattern>

A useful flag I ran into is -B. This flag allows context lines to be filtered as well as the specified pattern. The flag takes a integer parameter to specify how many context lines.

Looping:

Here is an example of a very simple for loop:

#!/bin/bash
for i in $(seq 1 10)
do
     echo $i
done

The above program will print 1 - 10 in the terminal.

⚠️ **GitHub.com Fallback** ⚠️