Mastering the Echo Command: Formatting and Control with -e and Escape Sequences
Photo by Caleb Steele on Unsplash
Introduction
Hey there! Welcome back to our blog. Today, we're going to dive into the versatile and powerful command known as "echo". ๐
Echo is a handy utility that allows us to display text on the command line or in scripts. It's simple to use and offers some cool features that can enhance your command-line experience. So, let's get started!
First things first, let's talk about the basic usage of the "echo" command. All you need to do is type "echo" followed by the text you want to display. For example, if you want to print "Hello Namaste", you would type:
[root@192 Desktop]# echo "Hello Namaste"
The "echo" command in the shell is a handy tool that allows us to display text or values on the screen. It's incredibly useful for printing messages, debugging, or even creating dynamic scripts. So let's get started and explore some of its cool features!
Printing Special Characters
Let's explore some special characters that can be used with the "echo" command to add some formatting or whitespace to our output. These characters are known as escape sequences and are prefixed with a backslash (\) to differentiate them from regular characters.
Escape Sequences :
\n -> New Line
\t -> Horizontal Tab
\v -> Vertical Tab
\b -> Backspace
\c -> Continue
Let's explore with examples
- \n: This escape sequence stands for a newline character, which moves the cursor to the beginning of the next line. For instance, if you want to print two lines of text, you can use it like this:
[root@192 Desktop]# echo -e "This is line one.\nThis is line two."
\t: The \t escape sequence adds a horizontal tab, which creates a horizontal indentation in your output. Here's an example:
[root@192 Desktop]# echo -e "Name:\tShubham Gour\nAge:\t22"
\v: The \v escape sequence inserts a vertical tab, which behaves similarly to the newline character but moves the cursor to the beginning of the next line with some additional vertical spacing. Try this out:
[root@192 Desktop]# echo -e "This is paragraph one.\vThis is paragraph two."
\b: This escape sequence represents the backspace character, which moves the cursor one character back. It can be useful for erasing or overwriting characters. Check out this example:
[root@192 Desktop]# echo -e "Pass\b\b\b\b\b\bHidden"
/c : suppress trailing new line with backspace interpreter โ-eโ to continue without emitting new line.
[root@192 Desktop]# echo -e "Hello \c"
Set Variables with and display value with echo
Let's touch on setting and deleting variables with the "echo" command. Variables are like containers that can hold different values. You can assign a value to a variable and then access that value whenever needed. Here's how you can set a variable using "echo":
[root@192 Desktop]# name="Shubham Gour"
[root@192 Desktop]# echo $name
In this example, we assign the string "Shubham Gour" to the variable "name" and then use "echo" to display its value.
Unset Variables and display with echo
To delete a variable, you can use the "unset" command. For instance try the below command:
[root@192 Desktop]# unset name
[root@192 Desktop]# echo $name
In the above when we hit echo $name we will get a space since the value is removed/deleted from the variable
Practical Questions on echo
Lets now see some practical use of echo command and unleash the power we learned till now
Question 1# : Run Multiple commands in same line
[root@192 Desktop]# echo "Hello" ; echo "World"
Question 2#: Print Message and content at the same time
[root@192 Desktop]# echo "Output of the command : " ; ls
Question 3#: Print sequence with echo command
[root@192 Desktop]# echo {1..10}{A..E}
Question 4#: Check exit status of the previous executed command
[root@192 Desktop]# echo $?
Here 0 means the command was executed successfully 1 and 2 meaning error in the output of the previous command
Let's explore System Variables
In the Linux operating system, system variables play a crucial role in managing and customizing the command-line environment. We will now unravel the significance of these variables and how they shape your Linux experience.
$PWD - Present Working Directory:
[root@192 Desktop]# echo $PWD
The PWD variable holds the absolute path to the current working directory. It helps you identify your current location within the file system. For example, running the command
echo $PWD
will display the full path to your current directory, such as/home/user/Documents
.$SHELL : Default Shell:
[root@192 Desktop]# echo $SHELL
SHELL denotes the default shell or command-line interpreter for the user. It specifies the program that processes your commands. To view your default shell, use the command
echo $SHELL
, which might output something like/bin/bash
or/usr/bin/zsh
Based on the default shell as per your config file$HOME : Home Directory:
[root@192 Desktop]# echo $HOME
HOME represents the path to the user's home directory. It serves as the default location when you open a terminal session or execute certain commands. To access your home directory, you can use the shortcut
cd ~
orcd $HOME
.$HOSTNAME - System Hostname
[root@192 Desktop]# echo $HOSTNAME
The HOSTNAME variable stores the name assigned to your system on a network. It helps identify your machine when connected to other systems. To view the hostname, simply type
echo $HOSTNAME
.$USER - Current User
[root@192 Desktop]# echo $USER
USER contains the username of the currently logged-in user. It provides a convenient way to retrieve your username programmatically. For instance, running
echo $USER
will display your username, such asroot
orshubham
.$0 - Current Shell Name
[root@192 Desktop]# echo $0
It helps identify the active shell environment. To see the current shell, you can utilize the command
echo $0
.
Understanding Linux system variables like $PWD, $SHELL, $HOME, $HOSTNAME, USER and $0 allows you to navigate and customize your Linux command-line experience effectively. These variables provide essential information about your environment, efficient navigation, customization, and scripting. By harnessing their power, you can enhance your productivity and efficiency on the Linux command line.