Table of Contents
If you have multiple consecutive spaces in the string, use the last solution, the
for
loop. You can use any of the following solutions if you have a multiline string.
Using tr Command
Use the tr
command to replace space with newline in bash.
1 2 3 4 5 |
text="This is a sample string." new_text=$(echo "$text" | tr ' ' '\n') echo "$new_text" |
1 2 3 4 5 6 7 |
This is a sample string. |
We initialized the text
variable with a sample string. Next, we piped the output of the echo
command to the tr
command, which translated (replaced) space (' '
) with newline (\n
).
We captured the output of the echo "$text" | tr ' ' '\n'
command using substitution syntax ($(...)
) and assigned to the new_text
variable. Finally, we used the echo
command to print the new_text
variable’s value on the bash console.
Using awk Command
Use the awk
command to replace space with newline in bash.
1 2 3 4 5 |
text="This is a sample string." new_text=$(echo "$text" | awk -v RS=" " '{print}') echo "$new_text" |
1 2 3 4 5 6 7 |
This is a sample string. |
This code snippet is similar to the previous example; however, we piped the echo
command output to the awk
command this time. The awk
is used for text processing. It operates per line by default, but we can customize it to work with fields and records (lines) using special actions and patterns.
In the above example, we used the awk
command to replace space with a newline. How? For that, we used the -v
option to set the record separator variable (RS
) to a space character (" "
). By default, the awk
takes the newline character as the value of the RS
variable.
So, modifying the RS
to a space character meant that each space-separated word would become a separate record for processing. For each record, the print
command was executed to print the current record, followed by a new line. This effectively replaced spaces with newlines in the output.
Alternatively, we can use the following solution:
1 2 3 4 5 |
text="This is a sample string." new_text=$(echo "$text" | awk '{gsub(/ /, "\n"); print}') echo "$new_text" |
1 2 3 4 5 6 7 |
This is a sample string. |
Here, we used the gsub, an AWK function to globally substitute all occurrences of a space character (/ /
) in the text
with a newline character ("\n"
).
Using sed Command
Use the sed
command to replace space with newline in bash.
1 2 3 4 5 |
text="This is a sample string." new_text=$(echo "$text" | sed 's/ /\n/g') echo "$new_text" |
1 2 3 4 5 6 7 |
This is a sample string. |
We used the sed
command, which is used to process and transform text, typically used for performing text substitution, deletion, insertion and more.
In the above example, we used the sed
command to substitute text. For that, we wrote the sed
command sequence enclosed in single quotes; for instance, 's/ /\n/g'
. This sed
sequence instructed to perform a substitution operation on the input text.
The sequence of the sed
command was comprised of various components; let’s learn them below:
-
s
(stands for substitute) – It told thesed
to find and replace. -
/
(delimiter character) – It separated the different parts of thesed
command: pattern to search for, replacement, and any flags. -
space
(between first and second/
): This was the pattern to search for. In our case, it was a single-space character. -
\n
(newline) – It was the replacement part; when the pattern (space) was found in the input, it would be replaced with a newline. -
g
(stands for global) – This flag toldsed
to perform the replacement globally, meaning all occurrences of the pattern in a line would be replaced with the newline character, not just the first occurrence.
This effectively split the input text into separate lines wherever there was a space character.
Using Parameter Expansion
Use parameter expansion to replace space with newline in bash.
1 2 3 4 5 |
text="This is a sample string." new_text="{text// /$'\n'}" echo "$new_text" |
1 2 3 4 5 6 7 |
This is a sample string. |
This example is similar to the previous ones, but we used parameter expansion this time. The parameter expansion ({text// /$'\n'}
) took the value of the text
variable, found all occurrences of a space character, and replaced them with a newline character. How? Following is a brief explanation of each component:
-
${...}
was the basic syntax of parameter expansion; here,...
meant anything that you want to accomplish; for instance,text// /$'\n'
in our case. -
//
was a pattern-matching operator used in parameter expansion. When used as${parameter//pattern/string}
, it matched all occurrences of the provided pattern in the parameter (in this case, the variabletext
) and replaced them with the given string. -
' '
was the pattern we searched for in thetext
variable. It was a single-space character. -
$'\n'
was an ANSI-C quoted string representing a newline character (\n
). The$'...'
syntax interpreted backslash escapes in bash, so$'\n'
was replaced with an actual newline character.
This was how we replaced space with newline in the text
.
Using for Loop
Use the for
loop to replace space with newline in bash.
1 2 3 4 5 6 7 8 |
text="This is a sample string." new_text="" for current_word in $text; do new_text="$new_text$current_word\n" done echo -e "$new_text" |
1 2 3 4 5 6 7 |
This is a sample string. |
We used a for
loop in the above example to iterate over the $text
. In each iteration, we concatenated the $new_text
, $current_word
, and a newline (\n
) as "$new_text$current_word\n"
and assigned its value to the new_text
variable.
Once the loop was over, we used the echo
command with the -e
option to print the value of the new_text
variable on the bash console.
Note that the -e
option was used to interpret backslash escapes in the given string (in this case, new_text
), allowing us to include special characters with specific meanings; for example, \n
means a new line.