Echo Command in Linux
One of the highly used yet simple linux commands to print simple output is echo. Linux echo command allows you to print Linux environment variables, create new files, add contents to an existing file and much more.
Print Linux Variable Value
To print stored value of a Linux environment variables
echo $SHELL
echo $HOSTNAME
To print variable value with attached characters, use { }
export TITLE=MR.
echo "$TITLEjohn" --> No output
echo "${TITLE}john"
Mr.john --> output
To print output of a command, enclose command in ( )
echo "Today's date is $(date)"
Print Text Containing Double Quotes
To print a double quote, enclose it within single quotes or use backslash
echo 'This is "Linux"'
or
echo "This is \"Linux\""
Print Text Containing Single Quotes
To print a single quote, enclose it within double quotes
echo "This is 'Linux'"
Print Text in Multiple Lines
To print text in multiple lines, use \n character with -e option
echo -e "Dear Sir \n Welcome"
Print Special Characters
Let's take you would like to actually print $NAME with echo command
echo "Use \$NAME variable"
Use $NAME variable --> output
The backslash (\) will print $ as is and will not consider $NAME as a variable
Create New File or Append
Use > to create a new file (or overwrite existing file)
echo "These are the file contents." > new_file.out
Use >> to append at the end of an existing file
echo "This is second line in the file." >> new_file.out
Print Output in Specific Colour
To print output in specific colour in Linux use below commands
echo -e "\033[1;37mThis is WHITE colour"
echo -e "\033[0;34mThis is BLUE colour"
echo -e "\033[0;32mThis is GREEN colour"
echo -e "\033[0;31mThis is RED colour"
echo -e "\033[0;33mThis is YELLOW colour"
echo -e "\033[1;30mThis is GRAY colour"
Enjoy! 🏀🏀🏀