Most shell commands produce their own output, which is displayed on the console monitor where the script is running. Many times though you will want to add your own text messages to help the script user know what is happening within the script. This is done using the echo command. The echo command can display a simple text string if you add the string following the command:$ echo This is a test
Notice that by default you don’t need to use quotes to delineate the string you’re displaying.
However, sometimes this can get tricky if you are using quotes within your string:
The echo command uses either double or single quotes to delineate text strings. If you use them within your string, you need to use one type of quote within the text, and the other type to delineate the string:$ echo "This is a test to see if you’re paying attention"
Now all of the quotation marks appear properly in the output.
You can add echo statements anywhere in your shell scripts where you need to display additional information:
When you run this script, it produces the output:$ chmod u+x test1
That’s nice, but what if you want to echo a text string on the same line as a command output? You can use the -n parameter for the echo statement to do that. Just change the first echo statement line to:
echo -n "The time and date are: "
You’ll need to use quotes around the string to ensure that there’s a space at the end of the echoed string. The command output begins exactly where the string output stops. The output will now look like:$ ./test1
Perfect! The echo command is a crucial piece of shell scripts that interact with users. You’ll find yourself using it in many situations, especially when you want to display the values of script variables. Let’s look at that next.
Shell Scripting Related Interview Questions
|Perl Scripting Interview Questions||Python Interview Questions|
|Linux Interview Questions||Linux Embedded systems Interview Questions|
|AWK Interview Questions||BioPerl Interview Questions|
|Sed (Stream Editor) Interview Questions||Advanced Linux Interview Questions|
|Unix/Linux Interview Questions||Unix Shell Scripting Interview Questions|
Shell Scripting Tutorial
Starting With Linux Shells
Getting To The Shell
Basic Bash Shell Commands
More Bash Shell Commands
Using Linux Environment Variables
Basic Script Building
Understanding Linux File Permissions
Working With Editors
Using Structured Commands
More Structured Commands
Handling User Input
Adding Color To Scripts
Introducing Sed And Gawk
The Ash Shell
The Tcsh Shell
The Korn Shell
The Zsh Shell
Using The Web
Using A Database
Shell Script For Administrators
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.