Shell Scripting – Tutorial 04: Bash Shell Scripts – Part 1

Shell Scripting – Tutorial 04: Bash Shell Scripts - Part 1

Shell Scripting – Tutorial 04: Bash Shell Scripts – Part 1

Today, in this post, we will continue the Tutorial 04 from our tutorial series on Shell scripting. In the previous ones, we addressed the following concepts: Terminals, Consoles, Shells, Bash Shell, Scripts and Shell Scripting.

For this reason, in this present tutorial we will focus a little more on the practical or technical part of the Script files created with Bash Shell.

Shell Scripting - Tutorial 03: All About Bash Shell Scripting

Shell Scripting – Tutorial 03: All about Scripting with Bash Shell

And, before starting this post called “Shell Scripting – Tutorial 04”, we recommend exploring the following related contents, at the end of reading this post today:

Shell Scripting - Tutorial 03: All About Bash Shell Scripting
Related article:
Shell Scripting – Tutorial 03: All About Scripts and Shell Scripting
Shell Scripting - Tutorial 02: All About Bash Shell
Related article:
Shell Scripting – Tutorial 02: All About Bash Shell

Shell Scripting Tutorial 04

Shell Scripting Tutorial 04

The Basics of Script Files

Generation

For generate a script filet basically you need to use a text editor, which can be without any problem, a simple one of Terminal (CLI) such as “nano” or “vi”, or from Desktop (GUI) such as "gedit" or "mousepad".

Also, they can be used source code editors more complex or robust IDE type, that detect the syntax of the language used, such as Geany, Atom, Sublime text, Visual Studio Code, among many others.

In principle, it would only suffice that, in one of them, we carry out the order of generate a new plain text file with or without the “.sh” extensionpreferably with her.

For example, the simplest thing would be to execute the following command in a terminal:

nano miprimerscript.sh

Execution

For run a bash shell script, you can choose 2 ways or forms, which are the following:

  • Invoke the Bash interpreter to run the script file:
bash miprimerscript.sh
  • Invoke the default interpreter (Sh) to execute the script file:
sh miprimerscript.sh

Comment: Keep in mind that, on some occasions, not invoking the correct Shell can cause partial or total malfunction of the created Script. Therefore, the ideal is that the Shell invoked in the first line of the Script is the one used to execute it. In our case, "bash".

However, we can also directly execute a script file as follows:

./miprimerscript.sh

In this case, the first 2 characters "./" indicate that we are going to execute the script file from the current directory, that is, the real path where the executable is.

Parts or elements within a Linux Script file

Really, a Script file in general is something very basic, therefore, it only consists of 2 items What are they:

  • She Bang or Sha-Bang (#!): This is the name given to the first line of the Script file, which has the purpose of specifying which program (Shell) it should execute. So that, and in the event that said program is not installed, an error is produced that prevents its execution.
  • The code: This can be anything from a single command to thousands of lines of code representing simple or complex commands in a Linux terminal.

For example:

#!/bin/bash
echo Mi Primer Script

Screen shots

Example on executing a Script - Screenshot 1

Example on executing a Script - Screenshot 2

Example on executing a Script - Screenshot 3

Example on executing a Script - Screenshot 4

Shell Scripting - Tutorial 01: The Shell, Bash Shell and Scripts
Related article:
Shell Scripting – Tutorial 01: Terminals, Consoles and Shells
PowerShell 7.2.6: Using Linux and Windows Commands in GNU
Related article:
PowerShell 7.2.6: Using Linux and Windows Commands in GNU

Abstract banner for post

Summary

In short, with this Tutorial 04 on «Shell Scripting» we already started the initial approach on more practical and technical aspects related to Script files generated with Bash Shell. Therefore, we hope that soon, they will start creating and using their first script files on GNU/Linux.

If you liked the content, comment and share it. And remember, visit the beginning of our «site», in addition to the official channel of Telegram for more news, tutorials and Linux updates.


Leave a Comment

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

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.