Goto - UNIX v6 Manual Questions

Hey :slight_smile: I am trying to understand the command goto.
I have some questions regarding the goto manual.

What does the underlined part mean?
I'm not allowed to type goto in the shell? But if goto is written into a file everything is okay?

So if a part of my file looks like this

...
goto end
...

Then he would look for a part like this

: end

But I have seen some scripts and I think they were from Unix v6, it looked everytime like this

end:

So what is now the right order?

Is it legal to write any other command in the line with the ":"?
For example

:end echo "Hello"

or

end: echo "Hello"

I'm not expecting that one person answers all 3 questions, you can pick the one you like.

Thank you for your help
and Merry Christmas :slight_smile:

Ps. I mean this manual: GOTO(1) - Manuals - Etsh Project (V6Shell)

This seems to me is referring to the historical Thompson shell (Its successor Bourne Shell in UNIX v7 did not have a goto...)

I think it means that you cannot use gotos if you use the shell interactively.
V6 Thompson Shell Port - Manuals - GOTO(1)

Presumably because there is a separate, external goto command that needs to go look for the goto label, once invoked

I would think that if the manual is explicit like that, only : end would be allowed ( : being the first character on the line) with any number of spaces. The other label is maybe from another script, perhaps sed ?
See also :
V6 Thompson Shell Port - Manuals - GOTO(1)

V6 Thompson Shell Port - Manuals - GOTO(1)

So it does seem that no commands will be processed on the line with the label.

2 Likes

The Thompson goto command was bizarre because it was not a shell built-in. It was actually an external command. Decisions like this were made to get the thing to run in a 16 bit machine.

First of all the label concept was piggy-backed onto the colon command. It is a command that does nothing. It still exists today in bash and ksh. I hope you have heard of it. You can give the colon command arguments which the shell "processes" but the command itself ignores. This was used to make the colon command a form of comment. The leading number-sign comment had not yet appeared. A comment like this:

: we should not do stuff like > /etc/passwd

was harmful because the shell would try to send the colon command's output to /etc/passwd. You had to be sure that your comments had no side effects. You could put the : command anywhere you want. The shell would know it should do nothing.

But a colon command used as label had additional restrictions becuase it had to be processed by /bin/goto. There were various versions of /bin/goto. Your label had to match whatever rules your version of /bin/goto had. The goto command used some magic to reposition the shell's input file. This means the input file has to be seekable. A file on paper tape is an example of a non-seekable file. A file on a deck of cards is another example.

The alternate syntax you have a seen "label:" is what csh uses.

Warning: I don't use goto's so maybe some of this is wrong.

1 Like

Thank you both for your great answers :slight_smile:

I have still some questions.

What does "interactively" mean? How do you use the shell interactively?

What is a label? Is it a string with or without spaces? Is "the end" also a label?
And I could use:

: the end

Is there a man page for colon command ( : )? I tried man colon, but nothing..

@Perderabo
4.
Why would it make a difference if goto was a shell built-in?

You can find a man page for : in the Man Pages section on this forum here: colon(1P)

1 Like

@1: By typing commands on the command line and executing them by using "enter".
@2: The man pages does not seem to specify whether or not spaces can be used, but I would just control the urge to use them.
@3: Note that it is piggy-backed on the colon command. If the shell arrives on that line, other than through a goto, the colon command gets executed and nothing happens. When you get to that line through the goto command, the colon acts as a label, and the actual colon command does not get executed..

1 Like

@4: If goto was a built-in in the shell, the shell might be able to find the labels in the script it was executing (and would be able to handle goto in an interactive shell) instead of needing to find and read the file containing the script.

1 Like

Again thank you for all the answers. :b:

Perderabo said:

Could you please explain this a bit more (regarding seekable).

The shell needs to search and find the target line for the goto in the input file, and jump there. Therefore, it needs to be randomly accessible, as are disk files.