How to represent a new line in a string in shell script?
时间: 2024-04-11 10:26:47 浏览: 7
In shell scripting, you can represent a new line in a string using the escape sequence `\n`. Here's an example:
```shell
#!/bin/bash
# Assigning a string with a new line
message="Hello,\nThis is a new line."
# Printing the string
echo -e "$message"
```
In this example, the escape sequence `\n` is used to represent a new line in the `message` string. The `-e` flag with the `echo` command enables interpretation of escape sequences, allowing the string to be printed with the desired line break.
When you run the script, it will output:
```
Hello,
This is a new line.
```
Note that the `-e` flag is necessary for `echo` to interpret escape sequences. If you omit it, the output will include the literal `\n` instead of a new line character.