I'm trying to use sed to insert a tab followed by a string with quotes (json) followed by a newline on mac on the second line. So far I've got

sed -i '.json' "2i\\
\"$(printf '\t') \"1.0.1\": \"1.0.1.zip\",\\
    " dummy.txt

and that outputs

"        "1.0.1": "1.0.1.zip",

and then when I try to remove the \"

sed -i '.json' "2i\\
$(printf '\t') \"1.0.1\": \"1.0.1.zip\",\\
    " dummy.txt

No tab is there. am i missing something here to get the tab?

Edit: I tried this


sed -i '' "2i\\
echo -e [\\t] \"1.0.1\": \"1.0.1.zip\",\\
    " dummy.txt

and the output of my dummy.txt file is

echo -e [t] "1.0.1": "1.0.1.zip",

No tab is present.

  • I can't see your problem. "none of them are working" and "does not work" is NOT something that people can build an analysis and meaningful answer on. Please post DATA: input, expected output, error msgs. Looking at your sed command, I can't see you "insert a tab" in it. Is it that you don't know how to include it into the script?
    – RudiC
    Commented Oct 24, 2018 at 20:27
  • For your second problem (same: DATA missing), looks like the $ sign, needed for expansion, is escaped thus useless.
    – RudiC
    Commented Oct 24, 2018 at 20:30
  • @RudiC fair enough. I added more description. Sorry if it wasn't clear before. this is my second day into bash so sorry if this is noob.
    – Crystal
    Commented Oct 24, 2018 at 20:39
  • JSON does not rely on tabs, nor newlines for that matter. What is the document you are trying to insert this key and value? It would be easier using a JSON processing tool such as jq.
    – Kusalananda
    Commented Oct 30, 2018 at 19:03
  • @Kusalananda it's an internal json document. i just thought it'd be nice to format it with sed.
    – Crystal
    Commented Oct 30, 2018 at 19:04

1 Answer 1


The problem is the single quotes. The shell interprets single quotes as "pass this on verbatim, without interpreting any special characters". You need to replace them with double quotes and escape the double quotes inside the parameter. Something like this:

sed -i "2i\\
\"1.0.1\": \"1.0.1.zip\",
   " file.json

For your original question, inserting a tab with sed, this works (2i will insert at line 2, where the first line of the file is 1). If you want to add a tab character in the command line (or in vim), press Ctrl+Q-<Tab> to tell it that the tab character is literal and not to be interpreted by the editor/shell. In the case of other editors, look in the editor documentation to see how to enter a literal tab.

The example would be:

sed -i.json "2i\\
\"1.0.1\": \"1.0.1.zip\",
<Ctrl-Q><Tab>" file.json

where the <Ctrl-Q><Tab> sequence means pressing those specific keys. This will depend on the editor that you are using. In the comments you mention that you are using VS Code, so instead of the <Ctrl+Q><Tab> trick you might want to use one of the options given in the answers to this question: https://stackoverflow.com/questions/45566785/vscode-insert-tab-character-manually

Note also that there is no space between -i and .json.

  • that doesn't work on mac. i get the error: command i expects \ followed by text the examples i see use single quotes like this in the post: stackoverflow.com/questions/25631989/…. but it doesn't work with the double quotes like you posted. would be glad to try something else?
    – Crystal
    Commented Oct 25, 2018 at 18:51
  • Oh sorry, I forgot that when you use double quotes you also need to escape the backslashes and add additional backslashes to the double quotes in the string itself. I've updated the answer accordingly. Commented Oct 25, 2018 at 19:04
  • Never mind that... what needed escaping was just the initial backslash. Try it now. Commented Oct 25, 2018 at 19:14
  • Did the last edit work? If so I would appreciate it if you marked it as the answer. Commented Oct 30, 2018 at 16:06
  • sed works in that i can replace the text i want. but my question was how to insert a tab with sed at a specific line. but yes i do mark questions as answered once they are answered.
    – Crystal
    Commented Oct 30, 2018 at 16:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .