sed insert line with leading spaces to a specific line

bash, linux, sed

Solution

You can escape the `space` character, for example to add 2 spaces:

sed -i "${line} i \ \ ${text}" $file

Or you can do it in the definition of your `text` variable:

text="\ \ hello world"

Problem

I have a line with spaces in the start for example " Hello world". I want to insert this line to a specific line in a file. For example insert " hello world" to the next file ``` hello world ``` result: ``` hello hello world world ``` I am using this sed script: ``` sed -i "${line} i ${text}" $file ``` the problem is that I am getting my new line without the spaces: ``` hello hello world world ```

Original source