Bash Goto Statement

Abid Ullah Feb 15, 2022
  1. What Is the goto Statement
  2. Is There a goto Statement in Bash
  3. the Different Alternatives of goto Statement in Bash
Bash Goto Statement

This article explains goto or a jump statement and its equivalent in Bash.

What Is the goto Statement

The goto statement is also called a jump statement, used to jump in the program from one code line to a specific code line within a function. For example, jumping from line 8 to line 5 to execute the program.

The goto statement sometimes is also considered as an unconditional jump. The use of the goto statement is referred a bad practice in programming because the goto statement makes the program hard to understand and difficult to modify.

Is There a goto Statement in Bash

No, there is no goto statement in Bash. Bash doesn’t have a goto operator.

Using the goto statement is a bad practice in programming but, it can be very helpful in solving different problems in Bash script. Using the goto statement helps us solve tasks in the Bash script.

We come up with some situations in Bash that are difficult to solve without using a goto statement. For example, we have a long Bash script that takes hours or days to run.

In this situation, we use the goto statement in the Bash script to save our time. Using the goto statement in the Bash script is very helpful when we debug the code.

the Different Alternatives of goto Statement in Bash

There is no direct equivalent in Bash but, there are different alternatives through which we can use a goto statement in the Bash script. We use the goto statement in the Bash script because it helps execute long scripts.

Using the goto statement in long Bash scripts helps us to save time. It is hard to deal with long Bash scripts if we don’t use the goto statement.

Let’s understand a Bash script that works like the goto statement in Bash. Consider the following code in Bash.

#!/bin/bash
function goto
{
    label=$1
    cmd=$(sed -n "/$label:/{:a;n;p;ba};" $0 | grep -v ':$')
    eval "$cmd"
    exit
}

start=${1:-"start"}

goto $start

start:
# your script goes here...
x=100
goto bar

mid:
x=101
echo "Mid is printed"

bar:
x=${x:-10}
echo x is $x

We create a Bash file by Vim editor using the command vi goto.sh and copy-paste the code above in the file.

Create goto sh file

Then we give executing permission to the file using the command chmod +x goto.sh.

Give executing permission to Bash file

We execute the Bash file using the command ./goto.sh.

Execute the Bash file

The output will give us x is 100. If we want to jump to the mid of the code, we need to use the command ./goto.sh mid.

Execute the mid of code of Bash file

The output will give us Mid is printed and x is 101. If we want to execute the code bar, we use the command ./goto.sh bar.

Execute the bar of code of Bash file

In the output, it will give us x is 10. In this way, the script works just like the goto statement.

Author: Abid Ullah
Abid Ullah avatar Abid Ullah avatar

My name is Abid Ullah, and I am a software engineer. I love writing articles on programming, and my favorite topics are Python, PHP, JavaScript, and Linux. I tend to provide solutions to people in programming problems through my articles. I believe that I can bring a lot to you with my skills, experience, and qualification in technical writing.

LinkedIn