bash goto code example

Example 1: bash jump to

#!/bin/bash
# include this boilerplate
function jumpto
{
    label=$1
    cmd=$(sed -n "/$label:/{:a;n;p;ba};" $0 | grep -v ':$')
    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!"

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

Example 2: bash jump to

#!/bin/bash

read -p 'Input something > ' one_thing

while true; do
  read -p 'Input something else > ' another_thing

  # Write some code to check if the requirements are met
  # Let's say in this case they are when the variable `thing_to_work` equals `done`

  if [[ "${thing_to_work}" == 'abcde' ]]; then
    break # Exit the loop
  else
    echo 'The requirements were not met, so the loop will start again'
  fi
done

Example 3: bash jump to

while :
do
  clear
  echo -n 'Questions? '
  read x
  switch $x in
    a)
      echo a
      ;;
    b|c)
      echo b or c
      ;;
    q)
      break
      ;;
    *)
      echo Invalid command \"$x\"
      ;;
  esac 
done