Bash Script Guide (with examples)

Bash is a popular scripting tool available in the . It’s short for Bourne Again Shell. It is a powerful tool for every Linux user or system administrator. So let’s start learning about Bash script on Linux!

Unix has 2 main categories of shells.

bourne shell is further classified as:

  • Korn shell (ksh)
  • Bourne shell (sh)
  • POSIX shell (sh)
  • Bourne Again shell (bash)

shell It is also further classified as:

  • C shell (csh)
  • TENEX (TOPS) C shell (tcsh)

Bash scripts are a very powerful and useful component for development. You can reduce short, repetitive tasks to a single line. Many long commands can be consolidated into a single executable code.

Bash is available on almost all versions of Linux and requires no additional installation. The list of available shells can be checked by typing the following command:

cat /etc/shells

The result will be something similar to this:

/bin/bash /bin/sh /bin/tcsh /bin/csh

Why use Bash scripts?

Bash functions can:

  • Eliminate repetitive tasks
  • Save time
  • Provides a well structured, modular and formatted sequence of activities
  • With scripts, we can provide dynamic values ​​to commands using command line arguments.
  • You can simplify complex commands into a single running unit
  • Once created, it can be run any number of times by anyone. Build once and execute many times.
  • Logic flows can be built using bash functions
  • Bash functions can be run at server startup or by adding a scheduled cron job
  • Commands can be debugged
  • Can have interactive shell commands

Bash is definitely a great tool to make your work easier and improve your projects. The potential uses are limitless, so today we’re just going to teach you the basics. Get ready to write your first bash script on Linux!

Getting started with Bash

Before you begin, you’ll need to access your server via SSH. If you’re having trouble doing this, see .

See also  All you need to know about wp-config.php

To get started with basic command options, you can refer to the bash man pages by typing:

man bash

Next, we will need to create a file .sh. For this we will use vim-editor. To create a file, use a command like this:

vim sampleFunction.sh

Now we will be taken to the archive .shwhere we can edit it.

This will generate output with the Bash commands and their usage. Every bash script on Linux must start with the following line:

#!/bin/bash

The following command displays the bash script path.

which bash

This will display the following output:

/bin/bash

Common bash syntax is:

function functionName { first command second command }

This can also be written as:

functionName (){ first command second command }

On a single line, this can be written like this:

functionName() { first command; secondcommand; }

An example of such a function is shown below, where we first create a directory and then change the path to point to the new directory:

sampleFunction () { mkdir -p $1 cd $1 }

$1 represents the input argument of the command line. Bash can create dynamic input within the command. To check this function, you can run:

sampleFunction myDir

Here myDir is a valid directory name. If you check the current working directory using the command pwdyou can see that you are currently inside the myDir newly created.

Likewise, any commonly used command can be added as a bash function.

Remember that when you are done using the VIM editor to edit the file .shyou can save and exit by pressing ESC to enter command mode, and then type :wq to save and exit.

Basic Bash Functions

One of the basic examples of the bash function in Linux is shown below:

#!/bin/bash testfunction(){ echo “My first function” } testfunction

If you save this script in testFunction.sh and you run it like ./testFunction.shyou will see the result as:

See also  hPanel vs. cPanel: everything you need to know

My first function

Echo prints the output to the console. If you swap the position of the function definition with the call, this will cause an error. The following snippet will give an error.

#!/bin/bash testfunction testfunction(){ echo “My first function” }

So, first you will have to define the function and then call it.

bash functions can accept any number of parameters. The following example accepts two parameters:

#!/bin/bash testfunction(){ echo $1 echo $2 } testfunction “Hello” “World”

You can also use interactive input and perform bash functions. One of these examples is shown below:

#!/bin/bash addition(){ sum=$(($1+$2)) return $sum } read -p “Enter a number: ” int1 read -p “Enter a number: ” int2 add $int1 $int2 echo “The result is : ” $?

In the example above, the value of addition is assigned in a variable sum, and this is what the function returns. Interactive input is taken using read for both numbers. Finally, the result is printed with $? which stores the $sum value generated by the function.

bash functions always return a single value.

You can leave comments inside the file by adding the symbol # to leave helpful notes.

bash scripts support:

  • Loop while
  • Loop for
  • Statement if
  • logic element and
  • logic element or
  • Statement Else If
  • Statement case

Below is a brief example of the While Loop.

#!/bin/bash isvalid=true count=1 while do echo $count if ; then break fi ((count++)) done

The example above uses the statements while Y if. This runs the loop while 5 times before exiting after checking the conditional statement if.

The result of this will be:

1 2 3 4 5

Loop for can be used to increment or decrement counters. An example of the loop for is the one shown below:

See also  .com domains |

#!/bin/bash for (( count=10; count>0; count– )) do echo -n “$count ” done

The output of this loop will be:

10 9 8 7 6 5 4 3 2 1

in Bash && represents the logical element AND, while || represents OR.

with the statements Ifwe can also define else if. Let’s look at an example:

#!/bin/bash echo “Enter a valid number” read n if ; then echo “This is the first number” elif ; then echo “This is second number” elif ; then echo ” This is third number ” else echo “No numbers over here” fi

The same example above can also be written using the statement case as it’s shown in the following:

#!/bin/bash echo ” Enter a valid number” read n case $n in 101) Echo ” This is the first number ” ;; 510) echo ” This is the second number ” ;; 999) echo “This is the third number” ;; *) echo “No numbers over here” ;; that C

in the statements case ;; represents a case break.

final words

This concludes our overview of basic bash script functions. Bash is a really powerful tool and can be easily learned. From here you can dive into creating more examples and have fun exploring more potential uses of bash scripts. Good luck!

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

Loading Facebook Comments ...
Loading Disqus Comments ...