≡ Menu

Cobol Hello World Example: How To Write, Compile and Execute Cobol Program on Linux OS

Question: I would like to understand the basics of how to write and execute Cobol program on Linux OS. Can you explain it with a simple example?

Answer: In this article, let us review very quickly how to write a basic Hello World Cobol program and execute cobol program on Linux or Unix OS.

1. Write a Hello World Cobol Program

Create the helloworld program using the Vim editor as shown below.

$ vim helloworld

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
* simple hello world program
PROCEDURE DIVISION.
    DISPLAY 'Hello world!'.
    STOP RUN.

Note: Comment in Cobol starts with *.

2. Make sure Cobol Compiler is installed on your system

Make sure Cobol compiler is installed on your system as shown below.

$ whereis cobc
cobc: /usr/bin/cobc /usr/share/man/man1/cobc.1.gz

$ which cobc
/usr/bin/cobc

Installing cobc compiler

If you don’t have cobol compiler, install it as shown below.

$ sudo apt-get install open-cobol

3. Compile the cobol program

Compile the HelloWorld which will create the HelloWorld executable.

$ cobc -free -x -o helloworld-exe helloworld

$ ls
helloworld  helloworld-exe*
  • -free – use free source format. Without this option cobol program requires certain format.
  • -x – build executable program.
  • -o FILE – place the output file into the specified FILE.

4. Execute the cobol Program

Execute by mentioning the program name.

$./helloworld-exe
Hello World!
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • jacobian February 26, 2010, 4:48 am

    very useful.thanks for sharing.but anyway I thought cobol is an obsolete programming language. 🙂

  • jumpingj February 26, 2010, 7:43 am

    Nice!!!

  • Catalin February 27, 2010, 3:13 am

    Cobol is not very old programming language. I see on http://en.wikipedia.org/wiki/COBOL. I think is more useful to try python … Anyway is a nice article .

  • David April 30, 2010, 7:53 am

    Ramesh as always great post,
    Two quick question:
    Does this compiler include Runtime? If not would you happen to know if there is any free Cobol Runtime licenses?

    Thanks in advance

    David

  • John August 19, 2010, 10:44 pm

    Compile fails with
    “cannot find lncurses”

  • Bhavesh Shah September 26, 2010, 12:11 pm

    i am getting Problem while creating and using Vim,Cobol compiler.
    Can any one help me out.

  • BigBert December 21, 2010, 6:39 pm

    Important note on Debian / Ubuntu machines:

    OpenCobol cannot find ncurse, so compile will fail. To fix this:

    Edit ld.so:

    sudo gedit /etc/ld.so.conf

    and add ‘/usr/local/lib’
    to the file.
    eg:
    include /etc/ld.so.conf.d/*.conf /usr/local/lib

    Create symbolic link to ncurses:
    sudo ln /lib/libncurses.so.5.7 /lib/libncurses.so

    Re-run ld config:
    sudo ldconfig

  • gowri shankar February 7, 2011, 11:38 pm

    how to run the cobol program in z/OS.

  • ashish March 28, 2011, 2:02 pm

    program is to be currect i know but how it will run on cobol compiler plz tell me.

  • idams h. chinwe May 31, 2011, 10:08 am

    A lot of lecturers make it difficult for students to learn COBOL but it will be easy if you allow student to learn directly via the way you solved that very simple HELLO program. I bet i learn’ t a bit from that program you just wrote.

  • Risk-Taker June 8, 2011, 10:21 pm

    Just wandering what is the best COBOL Compiler for college students? how to compile COBOL program and can you please give me a sample program based on that version of COBOL. Tnx

  • minwet September 9, 2011, 1:17 am

    The best compiler can be found in Pre historic caves package in adobe boxes.

  • sirisha October 28, 2011, 10:48 pm

    how to install cobol program

  • Oguilaya fidelis February 21, 2012, 12:20 pm

    I need some one to teach me a COBOL progame.

  • jha October 10, 2012, 11:44 pm

    i want to learn more about cobol ….and how to compile a cobol program?????

  • nardzkie October 10, 2012, 11:48 pm

    nice to learn more about cobol..i love it…can you give some other example of program by using cobol.,???

  • ATHI October 30, 2012, 12:49 pm

    why only linux or unix are recommanded , if we can use in other os like windows how can we use it?

  • Eddie Sullivan Jr June 1, 2013, 11:25 pm

    My I have information on how to install a cobo compiler and editor, the files

  • Xeno Campanoli October 24, 2013, 6:36 pm

    cobc helloworld
    helloworld:1: Error: Invalid indicator ‘F’ at column 7

    This is on my Ubuntu 13.04 box with:
    $ uname -a
    Linux xeno-ThinkPad-T410 3.8.0-32-generic #47-Ubuntu SMP Tue Oct 1 22:35:23 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

    I am using open cobol and I think I am following the instructions, so there must be another problem. Oh, the cksum of the file pasted from above is:

    $ cksum hw.cob
    521470958 141 hw.cob
    xeno@xeno-ThinkPad-T410:~/shop/flatshop/codfish/top/examples/hello$ cksum helloworld
    521470958 141 helloworld

  • kalesha May 20, 2014, 1:14 am

    Nice done

  • FoodForThought July 14, 2014, 6:46 pm

    DISPLAY ‘Hello, world’

    You see that “sentence” up there? Yup, that is actually an entire “hello world” program in COBOL…

    It’s so easy, a seasoned programmer can learn it in only 3-4 weeks.

    Oh, COBAL also runs about 70% of the worlds infrastructure… Yea, you heard that right. Everything from payroll to gas pumps & ATM machines to life support systems in the hospital. Pretty much anything mission critical that simply CAN NOT CRASH runs on COBAL to this very day.

    More lines of code are in use for COBOL than for all other programming languages COMBINED. The average American comes in contact with it 13 times a day on average!

    COBOL truly is the 10 ton elephant in the middle of the room. It’s ANCIENT but it works. It has been in place SO LONG that it cannot be replaced. Believe me, they have tried, over and over and over again and it just costs too much and whatever language they try to replace it with is just is not as reliable.

    And lastly, the programmers for COBOL are rapidly aging and retiring or already long gone… but COBOL isn’t going ANYWHERE. Not in YOUR lifetime.

    All that being said, you may want to take a closer look into it. COBOL is a seriously BORING language used for BORING stuff. But big companies NEED COBOL programmers like junkies need junk. They will PAY you TOP DOLLAR.

    And there’s no competition. Period. The trick is, knowing where to look…

  • Anonymous October 25, 2014, 9:00 am

    i found cobol very inrtresting with help of my lecturere and this acticle

  • Neo Krishna November 1, 2015, 9:44 pm

    In today’s time, if you want to stand out as a programmer then Cobol is the buzzword. I saw these old grey haired Cobol programmers who are earning 3 to 5 times higher then conventional Java or DotNet programmers…

    Micro Focus has also developed Visual Cobol IDE that works on top of Visual Studio or Eclipse IDE for RAD and Agile development…

    All of the world’s largest Banks and Insurance companies are running Cobol…

    Total number of searches on Google in a day are lesser than total number of transactions done in a Cobol system.

    The reason, right from the horse’s mouth – It’s so stable, it’s peace of mind at the end of the day!

  • robis54 December 13, 2015, 12:39 pm

    Hi ! , to awake this nice page I want to give my humble opinion!
    Cobol isn’t ready to die ! Like latin or greek, many people use dead languages and Cobol is very litteral and powerfull.
    On big systems but mot only, it’ s the stablest compared to many others. If somebody wants a short example more structured that “hello World”, I will write a bill more here !
    I’m very interest by this present article and thank the author RAMESH for having done it. There are old things which persist, Cobol is a strong survivor gracefull to IBM, yeah !!!
    It’s nice to speak about Linux and Open-Cobol , I like it, surely others too !

  • robis54 December 13, 2015, 6:14 pm

    Hi ! Dears, I have seen light in this site and entered again !
    Without a lot of comments let me share the following with you…
    As I promised here you can find a small Cobol program compiled with open-cobol in Linux Kubuntu with this command : cobc -Wall -x moreorless.cob.
    Then you find in your directory the program named moreorless. To start it just type ./moreorless in a konsole and enjoy !
    (I typed the code exactly like here down and saved it with the name moreorless.cob , the line numbers are to help positionning correctly the lines of code I find it cool !)
    Listing, here :
    000010 IDENTIFICATION DIVISION.
    000020 PROGRAM-ID. MoreOrLess.
    000030*REMARKS.
    000040* PLAYING MORE OR LESS.
    000050 DATA DIVISION.
    000060 WORKING-STORAGE SECTION.
    000070
    000080 77 seed PIC 9(8) VALUE 0.
    000090 77 nbRandomized PIC 999.
    000100 77 nbEntries PIC 999.
    000110 77 nbEnter PIC 999.
    000120 SCREEN SECTION.
    000130
    000140 1 pla-title.
    000150 2 BLANK SCREEN.
    000160 2 LINE 2 COL 15 VALUE ‘playing More Or Less !’.
    000170
    000180 1 pla-more.
    000190 2 LINE 4 COL 5 VALUE ‘It is more !’.
    000200
    000210 1 pla-less.
    000220 2 LINE 4 COL 5 VALUE ‘It is less’.
    000230
    000240 1 pla-found.
    000250 2 LINE 4 COL 5 VALUE ‘Hurra ! you got it !’.
    000260 2 LINE 8 COL 5 VALUE ‘Game OVER.’.
    000270 1 pls-nb.
    000280 2 LINE 6 COL 5 VALUE ‘Please enter a number : ‘.
    000290 2 PIC zzz TO nbEntries REQUIRED.
    000300
    000301 1 pls-ent.
    000302 2 LINE 10 COL 5 VALUE ‘Enter 0 to finish : ‘.
    000303 2 PIC zzz to nbEnter REQUIRED.
    000310 PROCEDURE DIVISION.
    000320
    000330 INITIALIZE nbEntries.
    000340 MOVE FUNCTION CURRENT-DATE(9:8) TO seed.
    000350
    000360 DISPLAY pla-title.
    000370
    000380 COMPUTE nbRandomized = FUNCTION RANDOM (seed) * 100 + 1.
    000390
    000400 PERFORM UNTIL nbEntries = nbRandomized
    000410 DISPLAY pls-nb
    000420 ACCEPT pls-nb
    000430
    000440 IF nbEntries > nbRandomized THEN
    000450 DISPLAY pla-less
    000460 ELSE
    000470 DISPLAY pla-more
    000480 END-IF
    000490 END-PERFORM.
    000500
    000510 DISPLAY pla-found.
    000520 DISPLAY pls-ent
    000521 ACCEPT pls-ent
    000530 STOP RUN.

    So, copy and past it in a small text-editor like notepad, wordpad, kwrite or gwrite or nano, vim and so on… and save it as is with the “name you want.cob” or “name you want.cbl”. Compile it and go with ./name (without extension)! I expect that you better this peace of code e.g. with a counter.
    (You will remark the formatting mode in the listing so that the position of procedure, label, command, remarks are respected).
    the rules to follow are like here :
    Standard ANS’85 Cobol programs are laid out as follows:

    Columns 1-6 Sequence number, may be left blank
    Column 7 Indicator column, may have a value of:
    * – line is a comment
    – – line is a continuation
    D – line is a ‘debugging line’
    Columns 8-11 Area A
    Columns 12-72 Area B
    Certain things need to, or can, start in Area A such as labels, and
    other things, such as statements, need to start in Area B.
    Some Cobol systems have extensions that allow free format source,
    OpenCobol does not.
    Well, I hope you will find your luck in this small “aparte”, thanks for your attention.

  • ajith January 21, 2016, 1:00 am

    it is very useful 4 me
    thank u all….!

  • govin shah1 March 24, 2016, 12:52 am

    Hi Team,
    I tried to execute a program in GNU cobol to check the precision of decimal point in a COMP-2 variable e.g 5/3. The result gets rounded up at the last digit.
    The result comes out to be 1.66666666666666667.

    Is there any compiler option or any setting in the configuration file to overcome this.

    The same program I executed in ZOS mainframe and the result comes up to be .16666666666666665E 01

  • Adam April 10, 2020, 9:25 am

    Just an FYI per the “Hello World” example, I tried it today (yes, several years after you published) and got an error on compilation step. Basically it was rejecting the * asterisk as the comment marker.
    Per https://stackoverflow.com/questions/25424250/cant-comment-and-other-problems-with-cobol
    if you use *> and then your comment text, it will be fine.