Shell Command / Script To Write Simple Output On Screen Under Linux and UNIX

How do I simply write output from my shell script to the screen under Unix / Linux BASH shell?

Most modern shell such as bash, ksh and others comes with the echo and print in built commands. There is also printf (C like statement) command to format and display data on screen

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
TimeN/A

ADVERTISEMENTS

echo command

All the parameters to the echo command are printed to the screen. For example:
echo "Hello World!"

You can add echo command to your shell script:

#!/bin/bash
echo "Memory information"
free -m
echo "Disk information"
df -h

printf command

You can also use printf command as follows
printf "Hello world\n"
Or use in a script as follows:

#!/bin/bash
print "Memory information\n"
free -m
printf "Disk information\n"
df -h

Where,

  • -n : It is a FORMAT controls the output as in C printf. \n is use to print new line.

More options

You can print a variable value with echo or printf command. First define a variable called DDAY:
DDAY="15-Aug-2007"
Now print a variable using echo command:
echo "$DDAY"
Output:

15-Aug-2007

Now print a variable using echo command:
printf "$DDAY\n"
Output:

15-Aug-2007

You can combine a shell variable with statements or command itself:

#!/bin/bash
DDAY="15-aug-2007" 
echo "D-Day is on $DDAY"
echo "Today is $(date)"
echo "Linux version : $(uname -r)"

$(uname -r) and/or $(date) are examples of command substitution. It allows the output of a command to replace the command name. The syntax comes in two different forms:

$(command-name)

OR

`command-name`

Preserving white spacing (blank space)

When you want the output to preserve your spacing enclose output in double quote:
$ echo "This is a test and today is $(date)"
Do not use single quote for command substitution:
$ echo 'This is a test and today is $(date)'
Sample outputs:

This is a test and today is $(date)

For more information read man page of printf and echo by typing the following commands:
$ man echo
$ man printf
$ man bash
$ man ksh

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
10 comments… add one
  • vivek Feb 11, 2007 @ 18:40

    Hi
    I was trying for a boot script on fc3 inorder to
    play an mp3 file on starting script time .But the GUI
    get stucked after logged in . Could you help me.

  • Lucas Aug 10, 2007 @ 14:25

    How can a simple shell interface respont to the following commands
    1.Clr-Clear Screen
    2.quit-quits the screen
    3.echo prints the comments on the screen followed by a new line
    4.copy file1.txt,file2.txt.create a copy of file1.txt with a new name file2.txt

  • somery.van Aug 19, 2008 @ 2:19

    Hello am very glad that this website so good.
    but i want you to do it well

    best regard,
    somery van

  • Anonymous Jul 22, 2009 @ 22:32

    Is there a way to print the results to screen every second (trying to check disk usage output without having to type df -k each time)

  • 🐧 nixCraft Jul 23, 2009 @ 2:07

    Try the following
    watch df -k

  • ratish singh Mar 15, 2010 @ 12:45

    how i can change my date format(dd/mm/yy) in my centos 5.

  • Nirav Aug 2, 2010 @ 12:06

    what is the output of command $ echo *…….please help me out

    Thanx in advance….send me results on my email id

  • Shimon Jun 15, 2013 @ 19:50

    Thank you.

  • Anup Apr 15, 2015 @ 13:06

    Hi,
    I want to develop a script which will provide the details of user logged into the Unix apps and report should sent through mail.

    can anyone help me on this within 24hrs :(

  • Keith Apr 26, 2016 @ 21:01

    Very nice article, thanks.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.