Imagine I have a script, which contains the goto command.

The goto command will search in the std input for a line like this : jumpHere.

But the std input is by default the terminal. It only makes sense if the file/script is the std input for the goto command. So where/who/what is changing the std input?

ps. I'm talking about old Thompson-Shell, Unix v6.


Note that goto was a separate utility, so not part of the Thompson shell per-se.

When you invoke the Thomson shell as:

sh the-script

sh opens the-script on stdin (fd 0) as if you had written

sh < the-script


The goto command will seek stdin back to the beginning (which obviously if stdin were a terminal and not a regular file wouldn't work) then look for the label in there and would leave the cursor in the file just after that. Then the shell would carry on from there.

If you wanted a command in your script to read from what stdin was originally, you'd use the <- special redirection operator:

cmd <- 

Note that with that same goto command, your script would also work with bash if you invoke your Thomson shell script as:

bash < the-script

ksh93 can emulate that goto with special seeking redirection operators. For instance this Thomson shell script:

echo Start
: start
ls -ld /proc/self/fd/0
sleep 1
goto start
echo End never reached

Could be written in ksh93 like:

echo Start
: start
ls -ld /proc/self/fd/0
sleep 1
exec <#((0)) <#": start"
echo End never reached

Both would run (provided you have the V6 goto command) with ksh93 when invoked as:

ksh93 < the-script
  • Sorry, I still do not understand how/where the std input was changed to the-script. :( I think you tell more about how goto in general works, but not how this works with the filedescriptors.
    – Joey
    Jan 5 '15 at 23:46
  • Now I understand! :) Is there any way you could change the std input then in the script to terminal?
    – Joey
    Jan 5 '15 at 23:54
  • 1
    @Joey. That's what the <- operator was for. Jan 6 '15 at 0:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.