I'm looking for a clean way to add multiple spaces to a sed statement. Having literal spaces in the statement works:

sed -i 's/this is a test/\n          and so is this/' test_file.txt

but I was hoping for a more precise way to accomplish this with regex. Thanks!

  • What do you mean by more precise? Do you mean that you want to insert an arbitrary number of spaces by using that number? Jul 25, 2018 at 13:37
  • Yes, so instead of 10 literal spaces, specifying the number "10"
    – GreNIX
    Jul 25, 2018 at 13:38

1 Answer 1


With bash or zsh, you can get an arbitrary number of spaces in a variable with:

printf -v spc %10s
sed -i "s/this is a test/&\n${spc}and so is this/" test_file.txt

With zsh, you can also do:

sed -i "s/this is a test/&\n${(l:10:)}and so is this/" test_file.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.