Wannabe Tutorials: Section 1 Part 3

(Valentine) #1


A Bash Script is a plain text file that can run commands that you would normally run in a command line. Pretty much anything that is a command or can be run in the command line can be run in a Script. In simpler terms imagine a Play. Every Play has a script that tells the actors what to do. In a sense this is what Bash Scripting is. In my last tutorial I briefly introduced Bash in this Script:


String= "Execute commands Y/N: "
read $String 

if [ Y $String ]; then 

String2= " Type file: "
read $String2

String3="Type directory: "
read $String3

echo ls $String3
echo mv $String2 $String3

echo "Press enter enter to exit."


echo "Press enter to exit. "


In Bash you don’t have to declare the variable, as you must in other languages. For example in C, this is how you would declare a int variable:

#include <stdio.h>
int main(void) {
    int a = 1, b = 2, sum;
    sum = a + b;
    printf("%d + %d = %d\n", a, b, sum);
    return 0;

In Bash, we don’t have to do this. But if we want the the input from the users we read with the read function. Read is equivalent to the scanf() function in C. The syntax for the read function is as follows:

Read (variable)

Simple yes, but for each variable that you read you must put a $ before the variable or you will get a error. So for the String variable the code would look something like this:

read $String

But you are not limited to the read command when working with variables. The echo command can do two things, print a string in the terminal or run a command. The echo command is the equivalent to the printf() and the system() function in the C language. The syntax looks like either:

echo “message”


echo command (variable)

As with the read function, you must put a $ in front of the variable that you insert into the function.


There are three loops in any programming language; for loop, while loop, and if-then loop. The most basic loop, if-then loop, is the loop that we will work with the most for now. The syntax for the if-then loop is as follows:

if [condition];then




Simple, but powerful.

Final words

That’s it for now. I hope I didn’t make it to cringy but I was starting to get bored and wanted to blow things up. All is all, cheers.


(oaktree) #2

You technically are “declaring” a variable when you set it equal to something. The variable is implicitly declared before it is initialized.



if-then is NOT a loop, it’s a conditional statement.