Menu

EOF character within bash

2015-06-26
2015-07-07
  • Eappen Nelluvelil

    Sorry in advance if this question has been asked before.

    I am working through K&R and I got stuck on the exercise with the functions getchar() and putchar(). I am not sure what the EOF character is defined as in bash. I tried Ctrl-D, which does not do anything, and Ctrl-Z, which just closes the shell. I am not sure what keys to hit to signify an EOF to the program.

     
  • David Grayson

    David Grayson - 2015-07-07

    Your post is missing a lot of details that could be helpful, like what terminal and compiler you are using. I suggest you use the "MSYS2 Shell" provided by MSYS2 and the "gcc" package, which compiles executables that run with the help of msys-2.0.dll, which is like Cygwin. When I do that (with MinTTY as my terminal and bash as my shell), Ctrl+D works fine as an end of file character.

    The test code I used to test this out was:

    #include <stdio.h>
    
    int main()
    {
        int c;
        while ((c = getchar()) != EOF)
        {
            printf("Got char %d\n", c);
        }
        printf("End of file.\n");
        return 0;
    }
    
     

    Last edit: David Grayson 2015-07-07
  • David Grayson

    David Grayson - 2015-07-07

    Another combination that works for me is the default Windows terminal, with bash (sh.exe), the MinGW-w64 Win64 Shell, and the compiler from the "mingw-w64-x86_64-gcc" package (which is invoked as simply "gcc" in that environment). The end of file character seems to be Ctrl+Z in that environment.

    To run bash in the default Windows terminal (just like the normal Windows Command Prompt) instead of MinTTY, you should make a copy of mingw64_shell.bat and add the line "goto startsh" right before the line that has "goto startmintty" in it. Then run that new BAT file.

     

    Last edit: David Grayson 2015-07-07