Open In App

Shell Scripting – Here Strings

Last Updated : 01 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Here String is a kind of string that is used for input redirection from text or a variable. In here string Input will be mentioned in the same line within the quotes. the string inside the quotes is known as the Here string and Its command contains “<<<” (3 less than the symbol). Here string and a heredoc are extremely similar, but the former is a lot more simplified version of the latter.
Because of this, a delimiter token is not required in this text. Whenever we need a quick technique to transform some strings into a command, it is typically preferred. It is also known as an ordinary string which is used for the purpose of input redirection, It’s not a special kind of string. The execution contains only a few steps.

Basic Syntax:

To construct a here-string, we use the “<<<” operator to redirect a string into a command. Concretely, the syntax is:

$ command <<< $DATA

What it essentially does is expand the variable DATA and redirect the output string to the COMMAND.

Usage of Here Strings

For input redirection from text or a variable, the here-string is utilized. On the same line, input is mentioned in single quotations (“).

Example 1: Basic Usage

#!/bin/bash
wc -w <<< 'Hello GeeksforGeeks\n'

 In this example, the string It is “Hello GeeksforGeeks\n” is called the here-string.

 

Output:

2

There are 2 words, so the output is been retrieved as 2.

Example 2: String with more words

#!/bin/bash
wc -w <<< 'Welcome to GFG a helping platform for CS Students'

In this example, the string “Welcome to GFG a helping platform for CS Students” is called the here-string.

 

Output:

9

There are 9 words, so the output is been retrieved as 9.

Example 3: Here String with Parameter

if grep -q "txt" <<< "$VAR"
then 
echo "$VAR contains the substring sequence \"txt\""
fi

 

In the above example, the here-string is present with a parameter $VAR which is working as a parameter.

Example 4: Escape Special Characters in Here String

word=$'HELLO\ngfg'
echo "$WORD"

It’s a kind of special character which is sometimes used in the here-string

 

The above example is here-string with Escape Special Characters and it’s Escaping some characters.


Similar Reads

Shell Scripting - Difference between Korn Shell and Bash shell
Korn Shell: Korn Shell or KSH was developed by a person named David Korn, which attempts to integrate the features of other shells like C shell, Bourne Shell, etc. Korn Shell allows developers to generate and create new shell commands whenever it is required. Korn shell was developed a long year back as it is older than the BASH shell and it has fe
3 min read
Shell Scripting - Restricted Shell
Shell is one of the most important and powerful tools available in GNU/Linux-based systems. One can control the entire system if used correctly, this exact feature makes the shell a double-edged sword. It can potentially harm the system if one executes a system command without knowing its underlying details and consequences. To prevent this we use
5 min read
Shell Scripting - Interactive and Non-Interactive Shell
A shell gives us an interface to the Unix system. While using an operating system, we indirectly interact with the shell. On Linux distribution systems, each time we use a terminal, we interact with the shell. The job of the shell is to interpret or analyze the Unix commands given by users. A shell accepts commands from the user and transforms them
3 min read
Shell Scripting - Default Shell Variable Value
A shell gives us an interface to the Unix system. While using an operating system, we indirectly interact with the shell. On Linux distribution systems, each time we use a terminal, we interact with the shell. The job of the shell is to interpret or analyze the Unix commands given by users. A shell accepts commands from the user and transforms them
3 min read
Shell Scripting - Shell Signals Values
Prerequisites: Processes, Bash Scripting, Shell Function Library Signals help the operating system to communicate with processes and vice-versa. Signals are also generated when processes don't function properly or try to access prohibited memory. Different signals are mapped to different numbers which are referred to as signal values. The Linux OS
6 min read
Introduction to Linux Shell and Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understanding shell scripting we have to get familiar with th
7 min read
Shell Scripting - Shell Variables
A shell variable is a character string in a shell that stores some value. It could be an integer, filename, string, or some shell command itself. Basically, it is a pointer to the actual data stored in memory. We have a few rules that have to be followed while writing variables in the script (which will be discussed in the article). Overall knowing
6 min read
Shell Script to Show the Difference Between echo “$SHELL” and echo ‘$SHELL’
In shell scripting and Linux, the echo command is used to display text on the terminal or console. When used with the $SHELL variable, which contains the path of the current user's shell program, the output of the echo command can be different depending on whether the variable is enclosed in single or double quotes. echo "$SHELL" will expand the $S
4 min read
Bash Scripting - How to Run Bash Scripting in Terminal
In this article, we are going to see how to run bash script in terminal. For example, we create a bash file set of instructions or commands ( known as bash script ), and through command, we can run this file in terminal. Let see how to run bash script in terminal. Example 1 : In this example we print or echo some text on screen with the help of a t
2 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 line ourselves (like ls or cp, for instance) and ot
6 min read
Article Tags :