Skip to content

Instantly share code, notes, and snippets.

@willkill07
Created April 4, 2015 21:15
Show Gist options
  • Save willkill07/377ea99ed01f44f0cb37 to your computer and use it in GitHub Desktop.
Save willkill07/377ea99ed01f44f0cb37 to your computer and use it in GitHub Desktop.
goto in bash (with reverse seeking)
#!/bin/bash
# include this boilerplate
function jumpto
{
label=$1
cmd=$(sed -n "/#$label:/{:a;n;p;ba};" $0)
eval "$cmd"
exit
}
start=${1:-"start"}
jumpto $start
#start:
# your script goes here...
x=100
jumpto foo
#mid:
x=101
echo "This is not printed!"
jumpto end
#foo:
x=${x:-10}
echo x is $x
jumpto mid
#end:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment