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

by Ramesh Natarajan on February 26, 2010

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!

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

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

{ 21 comments… read them below or add one }

1 jacobian February 26, 2010 at 4:48 am

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

2 jumpingj February 26, 2010 at 7:43 am

Nice!!!

3 Catalin February 27, 2010 at 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 .

4 David April 30, 2010 at 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

5 John August 19, 2010 at 10:44 pm

Compile fails with
“cannot find lncurses”

6 Bhavesh Shah September 26, 2010 at 12:11 pm

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

7 BigBert December 21, 2010 at 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

8 gowri shankar February 7, 2011 at 11:38 pm

how to run the cobol program in z/OS.

9 ashish March 28, 2011 at 2:02 pm

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

10 idams h. chinwe May 31, 2011 at 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.

11 Risk-Taker June 8, 2011 at 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

12 minwet September 9, 2011 at 1:17 am

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

13 sirisha October 28, 2011 at 10:48 pm

how to install cobol program

14 Oguilaya fidelis February 21, 2012 at 12:20 pm

I need some one to teach me a COBOL progame.

15 jha October 10, 2012 at 11:44 pm

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

16 nardzkie October 10, 2012 at 11:48 pm

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

17 ATHI October 30, 2012 at 12:49 pm

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

18 Eddie Sullivan Jr June 1, 2013 at 11:25 pm

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

19 Xeno Campanoli October 24, 2013 at 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

20 kalesha May 20, 2014 at 1:14 am

Nice done

21 FoodForThought July 14, 2014 at 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…

Leave a Comment

Previous post:

Next post: