Bash Scripting - Bash Echo Command
Last Updated :
18 Apr, 2024
In this article, we are going to see the echo command. The Echo command is a built-in command feature for Unix / Linux which is generally used to display the text or message on the screen.
Syntax :
$ echo [option][text]
For Example :
$ echo Geeks For Geeks
Output :
Geeks For Geeks
OutputThere are generally three options:
- -n: It does not print the trailing newline.
- -E: It is the default option that disables the implementation of escape sequences.
- -e: It is used to enable interpretation of backslash escapes
Some escape sequences perform different operations such as:
Escape Sequences | Meaning |
---|
\b | Backspace |
\\ | Backslash |
\n | New Line |
\r | Carriage Return |
\t | Horizontal Tab |
\v | Vertical Tab |
Example 1: Backspace
$ echo -e "Geeks \bFor \bGeeks"
Output:
GeeksForGeeks
OutputExample 2: New Line
$ echo -e "Geeks\nFor\nGeeks"
Output :
Geeks
For
Geeks
OutputExample 3: Horizontal Tab
$ echo -e "Geeks\tFor\tGeeks"
Output :
Geeks For Geeks
OutputExample 4: Backslash
$ echo -e "Geeks\\For\\Geeks"
Output :Â
Geeks\For\Geeks
OutputExample 5: Carriage Return
$ echo -e "Geeks\rFor Geeks"
Output :Â
For Geeks
OutputExample 6: Vertical Tab
$ echo -e "Geeks\v For\v Geeks"
Output :
Geeks♂ For♂ Geeks
OutputExample 7: -n Option
$ echo -n "Geeks For Geeks"
Output :
Geeks For Geeks
OutputYou can find all the commands related to echo by writing the following command.
$ /bin/echo --help
Output :Â
OutputThere are some other ways to use echo command
Taking input from user:Â
We create a text file named " userInput.sh " and write the following code inside the file.Â
#!/bin/sh
echo "Enter Your Name : "
read name #It take input from user
echo "Hello, $name. Welcome to GeeksForGeeks"
Now run the " userInput.sh " with the help of the below code:
$ chmod +x ./userInput.sh
$ ./userInput.sh
Output :
OutputConclusion
In conclusion, the echo command is a versatile and powerful built-in command in Unix/Linux systems that allows users to display text or messages on the screen. The article provided a detailed overview of the echo command's syntax, options, and various examples demonstrating its usage, including the ability to suppress newline characters, enable/disable escape sequence interpretation, and display special characters like backspaces, tabs, and carriage returns. The article also covered how to take input from users using the echo and read commands together. Overall, the echo command is an essential tool in a Unix/Linux user's toolkit, enabling effective text manipulation and output display within the command-line environment.
Similar Reads
Bash Scripting - Write Output of Bash Command into Log File Let there are some situations in which you have to save your output in a file ( generally called log file). Output can be user details ( username, password, Gmail, etc. ), products record ( buying or selling any goods ), or simply any kind of data that you can store in a log file. Let see how to wri
4 min read
Basics of Batch Scripting Batch Scripting consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. It is not commonly used as a programming language and so it is not commonly practiced and is not trending but its control and dominance in the Windows environment can never b
4 min read
Bash Script - Command Substitution In order to understand command substitution, let us first discuss substitution in shell scripts. Substitution is a functionality by following which we can instruct the shell to substitute the actual value of an expression. Example: In the program below we have firstly created variable str and assign
4 min read
Bash Scripting - File Extension Bash scripting is a powerful tool for automating tasks and working with files in the command line. One important aspect of working with files in bash is understanding how to handle file extensions. In bash, a file extension is the portion of a file name that follows the last period (.) in the file n
6 min read
Bash Script - How to use Command Line Arguments In this article, let us see about Command Line Arguments usage in Bash script. Arguments are inputs that are necessary to process the flow. Instead of getting input from a shell program or assigning it to the program, the arguments are passed in the execution part. Positional Parameters Command-line
4 min read
Bash Scripting - Working of Bash Scripting Bash Scripting is a crucial component of Linux's process automation. You can write a series of commands in a file and then execute them using scripting. A Bash script is a plain text file with a set of commands inside of it. These commands are a combination of ones we typically type on the command l
6 min read
Bash Script - Working of Bash Variables A Bash script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a reusability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can us
7 min read
Shell Scripting - Define #!/bin/bash A shell provides an interface to connect with the system. When we use an operating system, we indirectly interact with the shell. While using a terminal every time on any Linux distribution system, we interact with the shell. The main function of the shell is to interpret or analyze Unix commands. A
3 min read
Bash Scripting - While Loop A while loop is a statement that iterates over a block of code till the condition specified is evaluated to false. We can use this statement or loop in our program when do not know how many times the condition is going to evaluate to true before evaluating to false. Â Table of Content The Syntax of
15+ min read
Bash Scripting - Bash Read Password without Echoing back Generally, you can see on the login page, whenever we type a password either it show dot (â¢) or asterisk (*) instead of a password. They do so because to protect our system or account. In such a case, the login page read the password and in place of the password, it will show a dot or asterisk. And
3 min read