How To Write a Bash Script

What is Bash script

A bash script is a series of commands in a text file. All commands you use in the terminal can be used in bash script. It helps to automate repetitive tasks on the Linux filesystem, it can also be used for a variety of activities, including executing a shell command, running multiple commands simultaneously, modifying administrative tasks.

Getting Started

Important: All bash scripts should have the “#!” keyword or what they called “shebang” which is not read as a comment. The first line is also a place where you put your interpreter which is in this case: /bin/bash.

Use “chmod 700 <script_name>” — this will let you read, write, and execute (run) the script — but only your user. To run the script, use command ./<script_name>

Variables

  • To set a variable, it is important to not have space in the variable name and the value. The terminal will give you an error.

User Inputs

  • To get the user input, you need to use read command and set a variable name on the input.

Adding terminal commands

  • Using the $(), you can run your script with a terminal command.

Loop

  • While loop
  • For loop
  • IF ELSE Statement

Reference
https://linuxhint.com/30_bash_script_examples/

Leave a Comment

Your email address will not be published. Required fields are marked *