Banner of None

Bash scripting tutorials: part 1 - hello world and variables


Category: Shell Scripting

Date: July 2020
Views: 1.33K


Shell scripting is the language of Linux Command Line, it is the most used language under linux because you can assimilate and use all other langages in your scripts. you can use Python scripts, All the operating system programs and commands, you can even run php scripts right from your terminal or shell scripts.

it is preferable to write shell scripts in a text file with the extension ".sh" for a better text highlighting in the text editors like vim.
shell scripts are just sequences of commands that are executed by the shell interpreter, in our case the shell interpreter is Bash.

Note: Shell can be Bash, zsh, sh or fish... in these tutorials I will be talking about scripts intended for Bash, but there is not much difference between the shells except for sh. which is the legacy shell in all unix systems and it does not support much of the advanced scripting supported bty the other shells.

First Bash script

As the Tradition dictates in all programming langues. Our first program will be a hello world program.
Lets fire up a terminal and create a file and call it first.sh like so :

	
 touch first.sh

Lets write the next two lines in our file:

	
#!/bin/bash
echo "hello world"

Lets write the next two lines in our file:

The first line is called a shabang. it is an instruction to the current shell interpreter that is about to execute our file to use "Bash" as the program to interpret and execute our script. the 2nd line is our message to the world using the command "echo" and we will be using this command a lot.
then we make the file executable. we have the choice to not make it executabe. but then we must pass it as an argument to Bash every time we want to execute it by typing : "bash first.sh".

	
  chmod +x first.sh
  ./first.sh

Variables

All programming languages use variables, and Bash scripting is no exception. for it is the variables that makes the scriptes dynamic and adaptive to their envirement.in Shell scripting there are only two types of variables : integers and strings (of characters) . and Bash scripting has many built in features for maniputating string. we will make a full tutorial about them in future articles

using variables


we can make variables just by giving them a name the starts with with alpha-characters or the underscore character "_" and then asigning a value to the variable with the "=" symbol. like so:

	
var1=123
mytext1="hello world"
echo "$var1 is a number"
echo  $mytext1 is a string echoed without double quotes

we then use them by the dollar "$" sign followed by the variable name , we can use them in other commands like "echo" or any other command.:

	
123 is a number
hello world is a string echoed without double quotes

As you have seen we created our first program and we said hello to the entire world , then we created some variables. the good stuff are yet to come in future articles and then you will witness the full power of Bash scripting.



1.33K views

Next Article

0 Comments, latest

No comments.