Bash Echo Examples

bash Echo Examples

Bash has several built-in functions for printing output to the terminal. To print text or string data into the terminal, another command as input, or a file, one of the most popular commands is “echo.” There are several alternatives available for this command that can be used with it for certain uses. In this tutorial, the ‘echo’ command’s applications are explained through several examples.

Syntax: 

 

echo [ OPTION(S) ] [ STRING ]

 

Choices:

Option Description: -n Remove the newline character from the output.

-e Make the backslash(/) character functional.

-E Turn off the backslash(/) character’s functionality.

-versionPresent the version details –helpShow instructions about how to use this command.

Example 1: Employing the {echo} command in its default manner

You can use the {echo} command without any parameters. The script below prints the basic text “Learn bash programming from mylinuxblog.com” using the {echo} command.

 

$ echo "Learn bash programming from mylinuxblog.com"

 

The script will run and produce the following output.

 

Example 2: Using the -n option with the `echo} command

The following script uses the “echo” command with the “-n” option. This option removes the new line from the output.

 

$ echo -n "Ali is a good boy"

 

The script will run and produce the following output.

 

Example 3: Using the -e option and the `echo} command

The following script uses the “echo” command with the “-e” option. To do this, the backslash(\) function is enabled, and the output is produced by inserting a “tab” space every time the string contains the character “\t.”

 

$ echo -e "Perl\tis\ta\tpowerful\tand\tportable\tlanguage"

 

The script will run and produce the following output.

 

Example 4: Applying the -E option to the `echo} command

The following script uses the “echo” command with the “-E” option. This setting turns off the backslash (/) function. The following command will not function with the new line (\n) that was used in the text.

 

$ echo -E "I\nlike\nbash\programming"

 

The script will run and produce the following output.

 

Example 5: Using a variable within the `echo} command

By including the variable in the text, its value can be written with other strings in the `echo` command. The `echo} command that follows makes use of the $price variable. However, there is one thing to keep in mind when using the variable in the echo command: in order to read the value of the variable by using the {echo` command, the variable must be enclosed in double quotations (“”). The variable name will be shown as output and the variable value will not be interpreted if the echo command contains a single quotation mark (‘).

 

$ price="\$100"

$ echo 'The price of this pen is $price'

$ echo "The price of this pen is $price"

 

The script will run and produce the following output.

 

Example 6: The output is printed vertically by using the ‘\v’ command in the `echo} command

 With the `echo` command, all words in the text “MLB is a Linux based blog site” will be printed vertically.

 

$ echo -e "MLB\v is \v a \v linux \v based \v blog \v site."

 

The script will run and produce the following output.

 

Example 7: You can omit any portion of the text by using the ‘\c’ command in the `echo} command. By removing the tutorials and newline, the echo command “enhance your Linux knowledge from myLinuxBlog” will be printed.

 

$ echo -e "Enrich your Linux knowledge from myLinuxBlog\ctutorials"

 

The script will run and produce the following output.

 

Example 8: Use the `echo` command to print the names of all files and directories.

The current directory’s files and directories can be read using the {echo` command. The list of files and folders in the current working directory will be printed when this command runs with a ‘*’.

 

$ echo *

 

The script will run and produce the following output.

 

Example 9: Use the `echo` command to print the names of particular files.

The `echo` command can be used to print the current directory’s specific file list. The list of all text files in the current directory can be printed with the following command. Using the {echo} command, you can search any file or folder in this manner.

 

$ echo *.txt

 

The script will run and produce the following output.

 

Example 10: Using the bash script’s `echo` command

An example of using the `echo} command in a bash script can be found here. Make a “echoexpl.sh” file and insert the script below. The addition of a carriage return to the string is what’\r’ does. “\r” is used at the beginning and end of the string in this example. In this case, the `echo} command is used with the ‘-e’ option to activate the ‘\r’ function.

 

echoexpl.sh

#!/bin/bash

string="\rPerl is a cross-platform, open-source programming language\r"

echo -e "$string"

 

Script execution:

 

$ bash echoexpl.sh

 

After the script runs, the text value of the $string variable will be printed with a new line.

 

In Summary:

 you can find the outcome of any script by printing the relevant output. For this reason, print options are crucial to any programming language. This lesson covers using bash’s single print option. However, there is another command in bash that can accomplish the same thing: “printf.” After performing the examples in this tutorial, perhaps the reader will have a better understanding of the various applications of the {echo} command and be able to utilise it appropriately.

 

Leave a Reply

Your email address will not be published. Required fields are marked *