logo
  • Home
  • Test & Rank
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Mining
  • Marine
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Jobs
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

5+ “echo” Command Usage Examples in Linux

Posted on December 26, 2013 by Manish

This tutorial explains Linux “echo” command, options and its usage with examples.

echo – Writes character strings to standard output.

DESCRIPTION

advertisement

The echo command writes character strings to standard output. Strings are separated by spaces, and a new-line character follows the last String parameter specified. If no String parameter is specified, a blank line (new-line character) is displayed.

SYNOPSIS

echo [OPTIONS] [ String … ]

OPTIONS

-n
On BSD and some variants derived from BSD does not begin a new line after the echoed text.
-e
on some variants this option is necessary to enable recognition of escape characters in string

Escape Characters

The echo command recognizes the following escape conventions:

\a
Displays an alert character.
\b
Displays a backspace character.
\c
Suppresses the new-line character that otherwise follows the final argument in the output. All characters following the \c sequence are ignored.
\f
Displays a form-feed character.
\n
Displays a new-line character.
\r
Displays a carriage return character.
\t
Displays a tab character.
\v
Displays a vertical tab character.
\\
Displays a backslash character.
\0Number
Displays an 8-bit character whose ASCII value is a 0-, 1-, 2-, or 3-digit octal number.

EXAMPLES

advertisement

1. To write a message to standard output, enter:

$ echo Hi there
Hi there

2. To display a message containing special characters, enter:

$ echo -e "I'm at lunch.\nI'll be back at 1:00."
I'm at lunch.
I'll be back at 1:00.

Note: You must put the message in quotation marks if it contains escape sequences. Otherwise, the shell interprets the \ (backslash) as a metacharacter and treats the \ differently.

3. Wildcards

$ echo *
Desktop Documents ls-output.txt Music Pictures Public Templates Videos

The above example would list all the files and directories in the current directory.

advertisement

4. To add a single line of text to a file, enter:

$ echo Remember to set the shell search path to $PATH. >>notes

This usage adds the message to the end of the file notes after the shell substitutes the value of the PATH shell variable.

5. Arithmetic Expansion

$ echo $((2 + 2))
4

Arithmetic expansion uses the form:

$((expression))
where expression is an arithmetic expression consisting of values and arithmetic operators.

Arithmetic expansion only supports integers (whole numbers, no decimals), but can perform quite a number of different operations.

6. Command Substitution

Command substitution allows us to use the output of a command as an expansion:

$ echo $(cal)
December 2013 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
$ echo "$(cal)"
   December 2013      
Su Mo Tu We Th Fr Sa  
 1  2  3  4  5  6  7  
 8  9 10 11 12 13 14  
15 16 17 18 19 20 21  
22 23 24 25 26 27 28  
29 30 31

Note : We should take a moment to look at the effect of double quotes on command substitution. By default, word-splitting looks for the presence of spaces, tabs, and newlines (linefeed characters) and treats them as delimiters between words. This means that unquoted spaces, tabs, and newlines are not considered to be part of the text. They only serve as separators. Since they separate the words into different arguments, our example command line contains a command followed by four distinct arguments. If we add double quotes word-splitting is suppressed and the embedded spaces are not treated as delimiters, rather they become part of the argument. Once the double quotes are added, our command line contains a command followed by a single argument. The fact that newlines are considered delimiters by the word-splitting mechanism causes an interesting, albeit subtle, effect on command substitution.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.
« Prev Page - 5 “printf” Command Usage Examples in Linux
» Next Page - 5 “expr” Command Usage Examples in Linux

« 5 “printf” Command Usage Examples in Linux
5 “expr” Command Usage Examples in Linux »
advertisement

Deep Dive @ Sanfoundry:

  1. Python Programming Examples
  2. C Programming Examples on File Handling
  3. C Programming Examples
  4. C# Programming Examples on Delegates
  5. C# Programming Examples on Strings
  6. C++ Programming Examples on Set & String Problems & Algorithms
  7. Linux Questions and Answers
  8. C Programming Examples on Set & String Problems & Algorithms
  9. C Programming Examples on Strings
  10. Linux Command Tutorials with Examples and Explanations
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.