Latest

16 Examples of Echo Commands in Linux

By October 9, 2020 No Comments

 

Echo command in Linux is likely one of the broadly used command in day-to-day operations process. The echo command is a built-in command-line instrument that prints the textual content or string to the usual output or redirect output to a file. The command is normally utilized in a bash shell or different shells to print the output from a command. Echo command can be used regularly in bash shell scripts.

Syntax of echo command

$ echo [option] [string]

16 Examples of Echo Commands in Linux16 Examples of Echo Commands in Linux

On this information, we are going to clarify Linux echo command with 16 sensible examples.

1) Show a easy message on the terminal

To print a easy textual content message on the terminal with echo command , run the command:

$ echo Hiya Guys

16 Examples of Echo Commands in Linux16 Examples of Echo Commands in Linux

2) Print out the worth of a variable with echo command

Let’s assume that you’ve got initialized a variable x and assigned it a worth of 100 as proven

$ x = 100

You possibly can echo its worth by prefixing the variable title with a greenback signal as proven

$ echo The worth of x is $x

16 Examples of Echo Commands in Linux16 Examples of Echo Commands in Linux

3) Print a line of textual content comprising of double quotes

To print a line of textual content with double quotes with echo command, merely enclose the phrase in double-quotes between single quotes.

$ echo Hiya guys welcome to ‘ “Linuxtechi” ‘

16 Examples of Echo Commands in Linux16 Examples of Echo Commands in Linux

4) Show a line of textual content consisting of a single quote

When you want to print a line with a phrase containing a single quote, enclose the phrase inside double citation marks as proven:

$ echo Hey, “We’re” Linuxtechi, a neighborhood pushed web site.

16 Examples of Echo Commands in Linux16 Examples of Echo Commands in Linux

5) Redirect echo command output to a file

To redirect echo command output to a file as a substitute of printing it on the display use the better than( > ) and double better than ( >> ) operators.

Once you use the better than( > ) operator, the file can be overwritten. If the file doesn’t exist, it will likely be created.

$ echo Hey guys, Welcome to Linuxtechi > greetings.txt

16 Examples of Echo Commands in Linux16 Examples of Echo Commands in Linux

The double better than operator ( >> ) appends textual content to a file. For instance, so as to add an entry to the /and many others/hosts information use the syntax proven

$ echo 192.168.2.100  web-server-01 >> /and many others/hosts

6) Use echo command to match an identical information

You should utilize echo command with a wildcard character to return an identical information i.e information that bear the identical file extension. For instance, to print out all textual content  information with the .txt extension, run the command.

$ echo *.txt

16 Examples of Echo Commands in Linux16 Examples of Echo Commands in Linux

7) Record all information and folders within the present listing

The echo command can act because the ls command and checklist all of the information and folders in your present listing utilizing the wildcard as proven:

$ echo *

16 Examples of Echo Commands in Linux16 Examples of Echo Commands in Linux

8) Create new traces utilizing the echo command

Utilizing the backslash interpreter -e, you may manipulate how the road seems on the output. For instance, to print a brand new line, use the ‘n‘ escape character possibility as proven under:

$ echo -e  “Welcome nto nLinuxtechi  ncommunity”

16 Examples of Echo Commands in Linux16 Examples of Echo Commands in Linux

9) Create tab areas between phrases in a sentence

Aside from creating a brand new line, you may improve the spacing between phrases in a sentence utilizing the ‘t‘ or TAB possibility as proven.

$ echo -e  “Welcome tto tLinuxtechi  tcommunity”

16 Examples of Echo Commands in Linux16 Examples of Echo Commands in Linux

10) Mix new line and tab spacing choices in echo command

You possibly can mix the ‘n’ and ‘t’ choices concurrently proven:

$ echo -e  “ntWelcome ntto ntLinuxtechi  ntcommunity”

16 Examples of Echo Commands in Linux16 Examples of Echo Commands in Linux

11) Create vertical tab areas in echo command output

The v possibility allows you to create vertical tab areas as proven:

$ echo -e  “Welcome vto vLinuxtechi  vtcommunity”

16 Examples of Echo Commands in Linux16 Examples of Echo Commands in Linux

12) Mix new line and vertical tab areas in echo command

Moreover, you may experiment and mix the ‘n’ and ‘v‘ choices as proven.

$ echo -e  “Welcome nvto nvLinuxtechi  nvtcommunity”

16 Examples of Echo Commands in Linux16 Examples of Echo Commands in Linux

13) Use the carriage return possibility

The r possibility omits the previous textual content. For instance, the output of the command under omits the primary 2 phrases.

$ echo -e  “Welcome to rLinuxtechi  neighborhood”

16 Examples of Echo Commands in Linux16 Examples of Echo Commands in Linux

14) Truncate echo command output of textual content

To suppress any additional output and proceed with out going to the subsequent line use the ‘c’ possibility as proven:

$ echo -e  Welcome to Linuxtechi  ccommunity

16 Examples of Echo Commands in Linux16 Examples of Echo Commands in Linux

15) Take away all areas from the textual content string with echo command

Use ‘b’ possibility together with -e possibility in echo command to take away all of the areas from the textual content string, instance is proven under:

$ echo -e “Welcome bto bLinux bCommunity”
WelcometoLinuxCommunity
$

16) Echo command utilization in bash shell script

As we’ve got already said that echo command is regularly utilized in bash shell scripts. Instance of echo command utilization in shell script are listed under:

$ cat echo-bash.sh
#!/bin/bash
# Echo Command Utilization in Script
os_version=$(grep -i “PRETTY_NAME” /and many others/os-release  | awk -F’=’ ‘{print $2}’ | sed ‘s/”//g’)
no_cpus=$(lscpu  | grep  ‘^CPU(s):’ | awk -F”:” ‘{print $2}’ | sed “s/^[ t]*//”)
total_mem=$(grep MemTotal /proc/meminfo | awk -F”:” ‘{print $2}’ | sed “s/^[ t]*//”)
echo ‘OS Model :’ $os_version
echo ‘Variety of CPUs :’ $no_cpus
echo ‘Complete Reminiscence :’ $total_mem
$

Output of above script when it’s executed:

$ bash echo-bash.sh
OS Model : CentOS Linux 7 (Core)
Variety of CPUs : 4
Complete Reminiscence : 8008968 kB
$

And that’s it. Thanks for taking your time on this information. Hopefully, you at the moment are higher knowledgeable.

Additionally Learn : 14 Grep Command Examples in Linux

echo $ in unix shell script,linux echo command output to file,printf command in linux,print command output in shell script,ksh echo command,output of echo in unix,yes please command in linux,echo n php,echo -n bash,echo -e,echo syntax,man echo,echo home command in linux,echo modifiers,linux echo 0,echo here,what is chmod 777 filename.sh used for?,echo command is used for mcq,echo option in bash,bash ls command,bash info,cmands,and commands,basic bash commands,bash seq reverse,terminal control commands,bash administrative commands,bash system,communication and scheduling in unix,archive in bash,xargs arg-file,xargs example pipe,how to use the xargs command on linux,pass output of command as argument,xargs stat,find f xargs,echo command in linux,bash echo command output,linux echo to file,echo 1 linux,echo my custom command in unix,echo command bash