Echo Command in Linux with Examples (original) (raw)

Linux echo

The echo command is one of the most basic and frequently used commands in Linux. The arguments passed to echo are printed to the standard output.

echo is commonly used in shell scripts to display a message or output the results of other commands.

echo is a shell builtin in Bash and most of the other popular shells like Zsh and Ksh. Its behavior is slightly different from shell to shell.

There is also a standalone /usr/bin/echo utility, but usually, the shell built-in version will take precedence. We will cover the Bash builtin version of echo.

The syntax for the echo command is as follows:

There are a few points to consider when using the echo command.

echo Examples

The following examples show how to use the echo command:

echo Hello, World!  
Hello, World!  
echo 'Hello "Linuxize"'  
echo "Hello \"Linuxize\""  
Hello "Linuxize"  
echo "I'm a Linux user."  
echo $'I\'m a Linux user.'  
I'm a Linux user.  
echo -e "You know nothing, Jon Snow.\n\t- Ygritte"  
You know nothing, Jon Snow.  
    - Ygritte  
echo The PHP files are: *.php  
The PHP files are: index.php contact.php functions.php  
echo -e 'The only true wisdom is in knowing you know nothing.\nSocrates' >> /tmp/file.txt  

If the file.txt doesn’t exist, the command will create it. When using > the file will be overwritten, while the >> will append the output to the file.
Use the catcommand to view the content of the file:

cat /tmp/file.txt  
The only true wisdom is in knowing you know nothing.  
Socrates  
echo $USER  
linuxize  

$USER is a shell variablethat holds your username.

echo "The date is: $(date +%D)"  
The date is: 04/17/19  

Conclusion

By now, you should have a good understanding of how the echo command works.

If you have any questions or feedback, feel free to leave a comment.

If you like our content, please consider buying us a coffee.
Thank you for your support!

Buy me a coffee

Sign up to our newsletter and get our latest tutorials and news straight to your mailbox.

Write a comment