This post provides tutorials on Bash commands and programming with examples.

The course covers Bash and shell programming features, including variables, loops, conditional expressions, and operators.

Shell is a command-line interpreters, It is an application to provide commands to the different operating systems such as Linux, Unix, and Mac.

Bash is an enhanced version of shell, an layer between user and operating system function calls.

User types the commands via command line or batch of commands also called scripts.

Requirement

To follow this course, you should have a basic understanding of computer language, and the following are required:

  • Linux-flavored operating system
  • Basic knowledge of any programming language

What is Bash

Bash, short for Bourne Again Shell, is an open-source command-line shell interpreter and scripting language. It interprets user-entered commands, either interactively or from a script file.

It serves as an interface to call commands, enabling system function calls.

Threre are two types of bash modes

  • Interactive mode: Also called a command interpreter, it allows the execution of commands in the terminal. It executes commands sequentially if there are multiple commands.
  • Non-interactive mode: This refers to scripts, allowing you to write Bash syntax that contains a sequence of multiple commands for script execution.

What is the difference between Bash and shell

Shell, an alias for Bourne Shell, is a command-line interpreter for Unix and Linux OS. Bash, an alias for Bourne Again Shell, is an enhanced version.

What is bash scripting used for?

Bash scripting has multiple use cases, including:

  • Writing scripts to automate programming tasks
  • Synchronizing tasks to copy files
  • Executing cron jobs for scheduling

How do I write code in bash?

To write code in a Bash script, follow these steps:

  1. In the terminal, create a file using vi test.sh.
  2. Add #!/bin/bash at the top of the file.
  3. Add multiple shell code snippets.
  4. Save the shell file with a .sh extension.
  5. Execute the shell script using the ./test.sh command in the terminal.

Is bash coding language?

Bash executes commands from the terminal or a file. It is a programming language that operates in Unix/Linux kernel operating systems, containing all the features to write complete code.

Bash is a special type of shell that takes input from commands, runs the code, and processes the input, returning results.

Shell Types

There are different types of shells in a Unix OS.

Shell TypeAliasFirst Line
shBourne Shell#!/bin/sh
bashBourne Again shell#!/bin/bash
cshellC shel#!/bin/csh

| tcsh | TENEX C shell | #!/bin/tcsh | | | | ksh | Korn shell | #!/bin/ksh |

Difference between Command Line and Script in bash

Let’s see the difference between command line and scripts

Commands line options

  • Command line has a prompt which takes input from a user
  • Commands are not saved to a file.
  • It supports only one command at a time.

Script files

  • Support multiple commands in a single file
  • Prompt still can be written in the script file
  • Only one line in a file executes sequentially