
- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
Is there a goto statement available in bash on Linux?
Long story short, Linux’s bash doesn’t have goto statements and no information about the control structures exists in the official documentation. It should also be noted that we can make use of the break and continue statement to achieve the same behaviour that goto statement provides us.
A simple behaviour of the goto can be achieved with a few tweaks and using the simple if condition in bash.
The script will look something like this
# ... Code You want to run here ... if false; then # ... Code You want to skip here ... fi # ... You want to resume here ...
Another idea is to make use of a bash script that is slightly more complicated, then the one mentioned above but works like a charm.
#!/bin/bash function goto { label=$1 cmd=$(sed -n "/$label:/{:a;n;p;ba};" $0 | grep -v ':$') eval "$cmd" exit } startFunc=${1:-"startFunc"} goto $startFunc startFunc: x=100 goto foo mid: x=101 echo "Not printed!" foo: x=${x:-10} echo x is $x
Output
$ ./sample.sh x is 100 $ ./sample.sh foo x is 11 $ ./sample.sh mid Not printed! x is 101
- Related Articles
- Implement a Counter in Bash Script on Linux
- Check if Directory is Mounted in Bash on Linux
- Introduction to Bash Globbing on Linux
- PHP goto Statement
- Why is there no goto in Python?
- goto statement in C/C++
- Ensure Only One Instance of a Bash Script Is Running on Linux
- Here Document And Here String in Bash on Linux
- The Meaning of IFS in Bash Scripting on Linux
- Preserve Bash History in Multiple Terminal Windows on Linux
- Array Operations in Linux bash
- How to create a CPU spike with bash command on Linux?
- Extracting a substring using Linux bash
- Count lines in a file using Linux bash
- How can I use goto statement in JavaScript?

Advertisements