Skip to content

Instantly share code, notes, and snippets.

@f-steff
Created October 9, 2019 09:36
Show Gist options
  • Save f-steff/e3e63078f37cf061f580d203c8738a06 to your computer and use it in GitHub Desktop.
Save f-steff/e3e63078f37cf061f580d203c8738a06 to your computer and use it in GitHub Desktop.
Goto in bash - Brilliant for debugging!!
#!/bin/bash
# GOTO for bash, https://stackoverflow.com/questions/9639103/is-there-a-goto-statement-in-bash/52872489#52872489
# Based upon https://stackoverflow.com/a/31269848/5353461
function goto
{
local label=$1
cmd=$(sed -En "/^[[:space:]]*#[[:space:]]*$label:[[:space:]]*#/{:a;n;p;ba};" "$0")
eval "$cmd"
exit
}
start=${1:-start}
goto "$start" # GOTO start: by default
#start:# Comments can occur after labels
echo start
goto end
# skip: # Whitespace is allowed
echo this is usually skipped
# end: #
echo end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment