Introduction

The ability to automate tasks with Bash scripts in Linux is one of the operating system’s most powerful components.

.bat file is a windows batch file it contains a sequence of windows/dos commands. These cannot be used in a Unix shell prompt.

.sh file is a unix shell script it contains a series of unix commands. These cannot be ran in a Windows DOS or Powershell prompt.

An .exe is windows-only executable format, which may in-turn execute a .bat script or other compiled code. Also cannot be directly used in a Unix prompt.

Bash is a name of the unix shell, which was also distributed as the shell for the GNU operating system and as the default shell on most Linux distros.

Shebang

Every Bash script stars with a shebang.

First line of the script is the shebang which tells the system how to execute the script.

Alternative (and better) shebang is using environment variable. eg. #!/usr/bin/env bash.

Hello Bash Scripting

#! /bin/bash

echo "Hello Bash";

<aside> ⚠️ Semicolon (;) is used to mark the end of the line. Each command starts on a new line, or after a semicolon.

</aside>

Redirect to file

I/O redirection can be used to capture the output from the shell or the output of a file and send it to another file.

#! /bin/bash

echo "This is to enter a file" > file.txt;

# > to write
cat > file2.txt;
# >> to append
cat >> file3.txt;

Comments

Comments are ignored in the scripts.

They just exist in your script for a better understanding of the code.