About Linux FAQ

Browse More FAQs:

Shell command or script to write simple output on screen under Linux and UNIX

Posted by Vivek Gite [Last updated: February 6, 2007]

Q. How do I simply write output from my shell script to on screen under BASH shell?

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

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.
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) or $(date) are examples of command substitution. It allows the output of a command to replace the command name. There are two forms:

$(command-name)

OR

`command-name`

Preserving white spacing (blank space)

Also note that 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)'Output:

This is a test and today is $(date)

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

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Other Helpful FAQs:

Discussion on This FAQ

  1. vivek Says:

    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.

  2. Lucas Says:

    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

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.