The echo command in Linux is a built-in command that allows users to display lines of text or strings that are passed as arguments. It is commonly used in shell scripts and batch files to output status text to the screen or a file.
Syntax of `echo` command in Linux
echo [option] [string]
[options] = The various options available for modifying the behavior of the `echo` command
[string] = It is the string that we want to display.
Basic Usage: Displaying Text/String:
The most straightforward usage of the echo command is to display a text or string on the terminal. To do this, you simply provide the desired text or string as an argument to the echo command.
If we want to display “Geeks for Geeks”. We use the following command.
echo "Geeks for Geeks"
Options Available in `echo` command in Linux
NOTE :- -e here enables the interpretation of backslash escapes
1. \b : it removes all the spaces in between the text
echo -e "Geeks \bfor \bGeeks"
2. \c : suppress trailing new line with backspace interpreter ‘-e‘ to continue without emitting new line.
echo -e "Geeks \cfor Geeks"
continue without emitting new line
In the above example, text after \c is not printed and omitted trailing new line.
3. \n : this option creates a new line from where it is used.
echo -e "Geeks \nfor \nGeeks"
create new line
4. \t : this option is used to create horizontal tab spaces.
echo -e "Geeks \tfor \tGeeks"
creating horizontal tab space
5. \r : carriage return with backspace interpreter ‘-e‘ to have specified carriage return in output.
echo -e "Geeks \rfor Geeks"
In the above example, text before \r is not printed.
6. \v : this option is used to create vertical tab spaces.
echo -e "Geeks \vfor \vGeeks
create vertical tab spaces
7. \a : alert return with backspace interpreter ‘-e‘ to have sound alert.
echo -e "\aGeeks for Geeks
This command when executed, it will produce an alert sound or Bell.
8. echo *: this command will print all files/folders, similar to ls command.
similar to `ls` command
9. -n: this option is used to omit echoing trailing newline.
echo -n "Geeks for Geeks"
omit echoing trailing newline
10. Redirecting `echo` Output
The output of the `echo` can be redirected to a file instead of displaying it on the terminal. We can achive this by using the `>` or `>>` operators for output redirection.
echo "Welcome GFG" > output.txt
This will write the output of the echo command to the file name `output.txt`. File will be overwritten if it already exists.
If we want to append the output in an existing file, we use `>>` instead of `>`.
In this article we have discussed `echo` command in Linux which is a powerful and versatile tool and allows users to display lines of the text or strings on the terminal. Overall, we can say that by understanding the `echo` command experimenting with its features, we can effectively display message, variables, or any desired text in our Linux.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!