JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Bash remove newline from string

Author:JIYIK Last Updated:2025/03/23 Views:

This article explains how to remove newlines from a string in Bash.


Creating a string with newlines in Bash

Sometimes, it is required that a string does not have newline characters in it. Bash provides several methods to remove newline characters from a given string.

To run the example, first, create a string with a newline character. Let's create a new string using the following command:

DemoString=$'\nThis is jiyik.com\r   \n'
echo "|${DemoString}|"

The above command will create \nsa string with newline characters . See the output:

|
This is jiyik.com

|

Remove newlines from a string in Bash using Bashims

bashisms is that //you can use this to remove newlines from a string. This operator will take the argument you want to remove from the string.

Let's see how to //remove newlines from a string using:

echo "|${DemoString//[$'\t\n\r']}|"
echo "|${DemoString//[$'\t\n\r' ]}|"

There are two commands, one with an argument \t\n\rand another with an argument \t\n\rand a space character. The above command uses bashisms to remove the newline and space characters.

See the output:

|This is jiyik.com   |

|Thisisjiyik.com|

Remove Newlines from a String Using tr Command in Bash

trThe tr command works similarly to bashims; it removes characters from its arguments. The tr command is also used to replace characters in a string.

But for now, we can focus on the delete option we used in the command using -d.

Let's try an trexample of removing newline characters from a given string using the command:

DemoString=$'\nThis is jiyik.com\r   \n'
echo "|$DemoString|"|tr -d '\n\r'

The above command will try to remove newline characters from the given string. See the output:

|This is jiyik.com   |

We can also use trthe command to remove the extra space. See the example:

DemoString=$'\nThis is jiyik.com\r   \n'
echo "|$DemoString|"|tr -d '\n\r '

The command above will also remove spaces from the string. See the output:

|Thisisjiyik.com|

Remove newlines from a string using extglob option in Bash

extglobThe -p option also provides the ability to remove spaces and newlines. To use this option, first, make sure it is enabled in Bash.

extglobThere are many options available for different operations. See the table below.

Options illustrate
? Can be used to match one or more occurrences of a given pattern.
* Can be used to match zero or multiple occurrences of a given pattern.
+ Can be used to match zero or multiple occurrences of a given pattern.
@ Can match one of the given patterns.
! (pattern list) Can match anything except one of the given patterns.

Let us try an example to remove newline characters from a given string using extglob option. See the example:

shopt -s extglob
shopt extglob
DemoString=$'\nThis is jiyik.com\r   \n'
echo "|${DemoString%%*([$'\t\r\n '])}|"

shopt -s extglobis used to enable the extglob option in Bash, shopt extglob is used to display the status of extglob, and finally, the last command will remove newlines using extglob.

See the output:

extglob         on
|
This is jiyik.com|

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Remove the first character from a string in Bash

Publish Date:2025/03/23 Views:61 Category:OPERATING SYSTEM

Removing the first character of a string can be tricky in Bash because there is no built-in function that can do this directly. However, there are several ways to achieve this, such as using the sed command, the cut command, or substring pa

Replace characters in a string using Bash

Publish Date:2025/03/23 Views:83 Category:OPERATING SYSTEM

A common operation involving string literals is replacing individual characters or substrings within the string with other characters or substrings. In this article, we will look at several ways to do string replacement using the BASH shell

String comparison operators in Bash

Publish Date:2025/03/23 Views:98 Category:OPERATING SYSTEM

In this article, we will explain string comparison in Bash using if statement. The shell program that runs in Linux and provides a command line interface for users to execute different commands is called Bash shell. It is also used as the d

Convert a string to an integer in Bash

Publish Date:2025/03/23 Views:154 Category:OPERATING SYSTEM

This article will discuss string to integer conversion in Bash script. First, we will discuss the math operations on strings and then we will see how to convert a string to an integer. Mathematical operations on strings Let's start our disc

Get the length of a string in Bash

Publish Date:2025/03/23 Views:145 Category:OPERATING SYSTEM

In programming languages, the length or size of a data type plays an important role. It helps in list traversal and facilitates the extraction of useful information. The length is especially important when performing tasks that require trav

使用 Bash 替换字符串中的字符

Publish Date:2023/05/18 Views:159 Category:操作系统

在本文中,我们将研究使用 BASH shell 进行字符串替换的几种方法。涉及字符串文字的一种常见操作是用其他字符或子字符串替换该字符串中的单个字符或子字符串。

Bash 中的字符串比较运算符

Publish Date:2023/05/18 Views:218 Category:操作系统

在本文中,我们将使用 if 语句解释 Bash 中的字符串比较。运行在 Linux 中,提供命令行界面供用户执行不同命令的 shell 程序称为 Bash shell。

在 Bash 中将字符串转换为整数

Publish Date:2023/05/18 Views:167 Category:操作系统

本篇文章将讨论 Bash 脚本中的字符串到整数的转换。 首先,我们将讨论字符串的数学运算问题,然后我们将了解如何将字符串转换为整数。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial