Learn Complete Linux Bash Scripting in One Article
Contents
– Photo by Iván Rivero from Pexels
In this article I’ll be discussing about linux bash scripting (shell scripting) and I will cover every fundamental concept that you need to get started with bash scripting under linux environment.
Bash scripting is a critical skill for every programmer.
First of all you will need a linux machine to execute these scripts.
You can use any of the below techniques to get a linux machine:
- Install a linux distribution
- Install WSL
- Install Cygwin or MSYS
- Installing git for bash will provide a bash prompt but this prompt is not a complete linux environment
I’ll suggest you to install WSL2 with Ubuntu-2020.04 (same is installed on my machine).
This one article will cover all the basics and some advanced concepts about bash (shell) scripting, so read it completely.
Very Basics
1. Hello World
Every programmer start by writing a program to print ‘Hello World’ on the console.
Create a file named hello.sh
and open it with your favourite text editor (notepad, sublime, atom, vscode, nano, vim, etc).
Write the following code into the file and save.
|
|
Now run the script by using following command
|
|
This will print
Hello World :)
2. Comments
We can write comments by using #
symbol.
|
|
Hello World
3. What is shebang (#! /bin/bash)
The shebang is used if you run the script directly as an executable file (for example with the command ./script.sh ). In this case it tells the operating system which executable to run. It’s not required and has no effect if you for example write bash ./script.sh
or source the script.
We write the below line at the top of every script
#!/bin/bash
or
#!/usr/bin/env bash
Hello World with shebang
|
|
Hello World :)
4. Variables
Creating variables in bash script is as simple as
|
|
NOTE: Remember there is no space around = operator.
Hello World with variables
– hello2.sh
–
|
|
$ ./hello2.sh
Hello World from a variable
5. Command line arguments
We can pass arguments while executing the script and use them in the script.
A simple example is below.
– params.sh
–
|
|
$ ./params.sh "Vikash" "Villageprogrammer.blogspot.com"
Hello I am Vikash My website is Villageprogrammer
Parameter Name | Description |
---|---|
$0 | Script name and path |
$1 | First argument |
$2 - $9 | Second to ninth argument |
${10} - ${…} | More than tenth argument |
You can execute a standard linux command and store the result in the variable.
– params2.sh
–
|
|
$ ./params2.sh
Today is : Thu Feb 18 17:05:56 IST 202 Directory : /mnt/d/Xplore-Training/UNIX/bash-scripting/scripting
Controls
1. If statement
The syntax of if statement is similar to other programming languages like C, C++.
Syntax
|
|
Example:
–if.sh
–
|
|
$ if.sh
10 is less than 20
You can combine parameter and variable logic with if to write complex scripts.
2. if-else
Syntax
|
|
Example:
–if-else.sh
–
|
|
$ ./if-else.sh
10 is not equal to 50
3. if-elif-else
Syntax
|
|
Example:
–if-elif-else.sh
–
|
|
$ ./if-elif-else.sh
10 is less than 20
Looping
Repetition of a code block can be done using while loop and for loop.
1. while loop
Syntax
|
|
Example
– while.sh
–
|
|
$ ./while.sh
1 2 3 4 5
2. for loop
We can pass an array of elements from command line and use them in the script by creating an array
array=$@
Syntax:
|
|
Example:
–for.sh
–
|
|
$ ./for.sh 1 2 3
1 2 3
3. break and continue
–break-continue.sh
–
|
|
$ break-continue.sh "Vikas" "Andy" "Mark"
Hello, Vikas End Found for <break> loop terminated Hello, Vikas Skip found Hello, Mark for <continue> loop terminated
Working with environment variables
We can access any of the environment variable from the bash script.
–vars.sh
–
|
|
$ ./vars.sh
The path is : <very long list of all paths will be printed> The terminal is : xterm-256color The editor is : The Editor variable is empty
Environment variables and their meaning
HOME : user’s home directory PATH : executable binary’s path HOSTNAME : hostname of the machine SHELL : shell that is being used USER : user of this session TERM : type of command line terminal that is being used
write a program to print user’s name, computer name, and home directory
|
|
vikas@DAVE in [/home/vikas]
Functions in bash
There are two ways to create functions in bash :
1. Function declaration syntax with function keyword
-function1.sh
-
|
|
2. Function declaration without keyword
-function2.sh
-
|
|
3. Calling the function
To call the function we just need to use the name of the function, we do not use parentheses as we use in other programming languages (e.g. C, C++, Java, Python, etc.).
-function1.sh
-
|
|
$ bash ./function1.sh
Hello
4. What about passing parameters ?
It’s a two step process
- Access the parameter value
To access the parameter use :
$1
: for first parameter,
$2
: for second parameter,
… ,
$n
: for nth parameter.
You can either create a local variable and save the value bylocal variable=$n
or directly access the value by$n
(n is the parameter number 1, 2, 3, etc). - Pass the parameter value
To pass the value write the value after the function name while calling the function (seperate by space).
add 2 3
5. Complete example
-function.sh
-
|
|
$ bash ./function.sh
5 9 20
Working with prompt message
You can write a message when asking the user for input.
It can be done by using read
command.
-user-prompt.sh
-
|
|
$ bash ./user-prompt.sh
Hey, What’s your name ? Vikash Hello, Vikash
If you want the input to be in the same line use -p
..
-user-prompt2.sh
-
|
|
$ bash ./user-prompt2.sh
Hey, What’s your name ? Vikash Hello, Vikash
Now you have enough understanding to get started with bash (shell) scripting. Go ahead an read a book or two on bash scripting and you will be good at it.
Keep Learining, Keep Practicing. :) :)
That’s All. 😅
Thanks for reading. 🙏
If you liked it feel free to share with your dear ones.💗
And tell me what do you think in the comment box.💬
Stay tuned with CS111.
This post was originally posted on Village Programmer
Author Vikash Patel
LastMod May 4, 2021