Is there a "goto" statement in bash?

kofucii picture kofucii · Mar 9, 2012 · Viewed 393.9k times · Source

Is there a "goto" statement in bash ? I know It is considered bad practice, but I need specifically "goto".

Answer

Michael Rusch picture Michael Rusch · Aug 2, 2013

If you are using it to skip part of a large script for debugging (see Karl Nicoll's comment), then if false could be a good option (not sure if "false" is always available, for me it is in /bin/false):

# ... Code I want to run here ...

if false; then

# ... Code I want to skip here ...

fi

# ... I want to resume here ...

The difficulty comes in when it's time to rip out your debugging code. The "if false" construct is pretty straightforward and memorable, but how do you find the matching fi? If your editor allows you to block indent, you could indent the skipped block (then you'll want to put it back when you're done). Or a comment on the fi line, but it would have to be something you'll remember, which I suspect will be very programmer-dependent.