Input
To take an input from the user, read function is used. In the following script, the value of name variable is taken from standard input device (keyboard) using the line read name
Output
To print messages to the terminal, the commonly used commands are printf and echo. The command echo is generally used to print a string with very simple formatting, whereas printf provides the scope of printing highly formatted output in flexible way.
Printing a String using echo
First, we will discuss different ways of use of echo command.
If you have a string in one line, you can put the string just after an echo. You may or may not include it in a single or double quote.
But if you have a string written in multiple lines, you must include it in a single or double quote.
Embedding Variables
In the following example, we will discuss how to embed variables.
Including Escape Sequence
Similar to C programming language, in shell script escape sequences can be used. In the following example, we have shown how to use escape sequences in a script.
A Script to print files in the current directory and their types using echo
A Script to print files in the current directory and their types using printf
Output Redirection
Redirecting to Terminal and Screen
Command tee is used to redirect output to the screen as well as the file.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.