echo command
echo is a command used to print a line of text or a string to the terminal. echo command is not limited to *nix operating system. They are also used in other operating systems like Unix, DOS, MS Windows etc. In Linux echo is usually used in shell scripts to display output while running a script. The syntax of echo command is as follows
echo [option] string
Examples
#1)Using echo command to display Hello World in terminal
echo Hello World
Sample Output
Hello World
#2) In above example we used echo command to display the text Hello world on terminal. Similarly echo can also display output of a command along with or without a text. To display the command output, the command should be entered within back quotes (`). For example to display current date and time, type
echo Current date and time is `date`
Sample Output
Current data and time is Mon Nov 24 16:29:00 IST 2014
Here echo displays the text "Current date and time is" and executes the command date.#3) echo command can be used to display environmental variables and variables
echo $HOME
Sample Output
/home/calypso
x=10
echo $x
Sample Output
echo $x
10
In this case we are setting the value of a variable x to 10 and to display the value of variable x we are using echo command#4) echo command can be used create an empty file, a new file with data, over-write a file and append data to a file
#5) Using echo we can display the arithmetic operations
echo $((3*4))
Sample Output
12
In this case the actual calculation is made by $(()) construct. echo command is just a means to display the output
echo 'sqrt (64)'|bc
Sample Output
8
The above example shows the method to find the square root of 64. The output of echo command is fed as the input to bc command which does the actual calculation#6) echo command can display coloured output using ANSI escape codes
In the first example \033 is the escape code followed by [32m where 32 is colour code of GREEN (foreground). So \033[32m will set green colour to the presiding string. At the end \033[0m is given to reset the color to the normal.In the second example \033 is the escape code followed by [1;31m where 1 is the code for bold text and 31 is the code for red colour. At the end \033[0m will reset the output to normal mode.
For more details on echo command visit the man pages
Comments
Post a Comment