GNU sed to BSD sed for bash goto function

55 views Asked by At

I make extensive use of a bash goto function that works through a GNU sed command. However, the sed command is not compatible with BSD sed. How would I need to change it to function correctly in an operating system that uses BSD sed, such as MacOS?

#!/bin/bash
scriptDir="$(cd "$( dirname "$BASH_SOURCE")" && pwd)"

function goto
{
    shortcutName=$1
    newPosition=$(sed -n -e "/: $shortcutName$/{:a;n;p;ba};" "$scriptDir/$(basename -- "$BASH_SOURCE")" )
    eval "$newPosition"
    exit
}
alias :="goto"

# script code

: somegotolabel

# script code

goto somegotolabel

Thanks in advance!

0

There are 0 answers