Bash can add quotes to each line with a sed or awk one-liner, but the result is only valid for one specific case: a plain list with no embedded quotes, commas, or backslashes. The moment a line contains a character that the target format treats specially — a double quote in JSON, a single quote in SQL, a comma in CSV — the one-liner silently produces broken output that compiles, runs, or imports incorrectly. JSON requires backslash-escaping embedded double quotes per RFC 8259, SQL doubles single quotes per the ISO rule used by PostgreSQL and SQLite, and CSV doubles embedded double quotes per RFC 4180. Bash has no built-in awareness of any of those rules. Understanding the difference between wrapping a line and correctly escaping it is the step that turns a working demo into paste-ready output, and it is exactly what the Add Quotes to Each Line tool handles for each preset.

bash add quotes to each line
Bash Add Quotes to Each Line: Sed, Awk, and Xargs

Common Bash One-Liners That Wrap Each Line in Quotes

Four shell idioms cover most "wrap each line in quotes" tasks in Bash: sed, awk, a while-read loop, and xargs. Each one takes a list, one item per line, and emits the same list with every line wrapped in a fixed pair of quote characters. For a deeper awk-focused walk-through of this specific path, see the awk add quotes to each line guide.

The shortest sed form uses the & metacharacter, which expands to the entire matched line:

sed 's/.*/"&"/' list.txt > quoted.txt

Awk reaches the same output with a print statement that concatenates literal quotes around $0, the whole current line:

awk '{ print "\"" $0 "\"" }' list.txt > quoted.txt

A while-read loop is more verbose but does not fork a subshell for each line, which matters when the input has hundreds of thousands of entries:

while IFS= read -r line; do printf '"%s"\n' "$line"; done < list.txt > quoted.txt

The IFS= and -r flags matter: without them, leading and trailing whitespace get stripped, and backslashes get interpreted as escape sequences. xargs can also do the job when you feed it one input line at a time with the -I{} placeholder:

xargs -I{} echo '"{}"' < list.txt > quoted.txt

All four produce visibly correct output when the input is a clean list of IDs, names, or filenames with no embedded quotes, commas, or backslashes. The problem appears only when the input stops being clean.

Where the Bash One-Liner Quietly Breaks

The sed and awk forms wrap every line in literal quote characters. They do not change the content of the line at all, which sounds harmless until the content includes a character that the target format treats specially.

Take a list that includes the line say "hi". Sed wraps it to "say "hi"", which is exactly the kind of output that breaks a JSON parser. The Bash one-liner never sees that anything went wrong, because the shell is happily passing bytes through. The reader of the file is the one that fails — usually with a syntax error far from the line that actually broke.

The same problem hits SQL, where a value containing a single quote, such as it's, needs to be wrapped as 'it''s'. A sed one-liner that wraps every line in single quotes produces 'it's', which closes the string literal after it and leaves the rest of the line as broken SQL. CSV has its own variant: a value like 3" monitor must become "3"" monitor" with the inner quote doubled, otherwise most importers either truncate the field at the inner quote or shift the columns.

Other quiet failures include empty lines (wrapped as "", which is not always what you want), leading and trailing whitespace that becomes significant inside a quoted field, backslashes in the source that Windows paths and regex strings often contain, and Unicode characters that some downstream tools read as a different byte sequence than what sed emitted. None of these problems are visible in the Bash output. They surface later, in a parser, a query, or a spreadsheet, after you have moved on.

How to Add Quotes to Each Line in Bash, the Right Way

  1. Decide which format the output needs to land in: JSON array, SQL IN (...) clause, CSV row, or something else. The escaping rule is different in each case, and choosing the wrong one is the entire failure mode.
  2. If the list is dirty — duplicates, empty lines, mixed whitespace — clean it first with Bash. A few one-liners handle most cases: sort -u list.txt # deduplicate awk 'NF' list.txt # drop blank or whitespace-only lines sed 's/^[[:space:]]*//; s/[[:space:]]*$//' list.txt # trim each line Pipe the cleaned list to the next step.
  3. Open Add Quotes to Each Line in your browser. The tool runs locally on your machine — no upload, no account, no network round trip after the page loads.
  4. Paste the cleaned list into the input area, one item per line.
  5. Click the preset that matches the format you need. JSON array wraps each line in double quotes, joins with commas, and surrounds the result with square brackets. SQL IN wraps each line in single quotes, joins with commas, and surrounds the list with parentheses. CSV row wraps each line in double quotes, doubles any embedded double quote, and emits the whole row on a single line for paste into a spreadsheet import. Plain quotes wraps each line in the chosen quote character with no surrounding brackets.
  6. Adjust the controls if the preset default is not exact: change the quote character, switch the delimiter, toggle escaping on or off, skip blank lines, trim each line, or add a trailing delimiter for formats that want one. The last-line-has-no-comma behaviour is the default and the right choice for JSON and SQL.
  7. Copy the result with the copy button. The tool reports the line count it processed, which you can compare against your input as a sanity check.

If you need different settings after running the tool once, start from your original list rather than the previous output. Running the tool on its own output wraps it again, because the tool cannot tell whether an existing quote is content or previous wrapping.

JSON, SQL, and CSV: Three Different Escaping Rules

The same input produces different, correct output under each preset. The table below summarises what each format actually requires, drawn from the standards they cite.

Format Quote character Embedded-quote rule Output wrapper
JSON array Double quote " Escape with backslash: " becomes \" per RFC 8259 [ ... ]
SQL IN list Single quote ' Double the quote: ' becomes '' per the PostgreSQL lexical rules and the ISO standard ( ... )
CSV row Double quote " Double the quote: " becomes "" per RFC 4180 single line, comma-joined
Backtick (JavaScript template) Backtick ` Escape with backslash: ` becomes \`, and the interpolation opener ${ is escaped to prevent expression injection joined with chosen delimiter

A line containing say "hi" becomes "say \"hi\"" in JSON, 'say "hi"' in SQL (double quotes are not special inside single-quoted SQL strings, but embedded single quotes double), and "say ""hi""" in CSV. The three rules are different from one another, and the difference is exactly what each preset encodes.

When Bash Is the Wrong Tool for the Job

Bash is the right tool when the input is clean and the target is also Bash, a shell pipeline, or a flat text file. A list of file paths being passed to rm or ls does not need per-format escaping, and a sed or awk one-liner is exactly the right level of effort.

Bash is the wrong tool when the list needs to land in code or data that has its own escaping rules. The shell cannot tell that JSON wants backslash-escaped quotes, that SQL wants doubled single quotes, or that CSV wants doubled double quotes. Reaching for a clever sed expression to handle all three usually produces a one-liner that nobody on the team can read six months later, including the person who wrote it.

For those cases, the Add Quotes to Each Line tool is built around exactly this distinction. The presets are pinned against the standards they cite, so the escaping is the documented behaviour rather than something reconstructed by hand. Input is capped at one million characters and processes in a single linear pass, so even very long lists return instantly. Everything runs in your browser, which is the right default for the kind of ID list or name list that you would rather not paste into a random website.

Useful Controls in Add Quotes to Each Line

Every preset exposes the same underlying controls, so you can adjust the result without leaving the page.

  • Quote style: double, single, backtick, or none (for cases where you only want a delimiter change).
  • Delimiter: comma is the default, but a space, semicolon, or pipe works for formats that use a different separator.
  • Trailing delimiter: off by default, so a JSON array does not end with a stray comma. A toggle adds it for formats that want one.
  • Escaping: on by default for the JSON, SQL, and CSV presets. Turning it off produces raw wrapping without character changes.
  • Skip blank lines: drops empty or whitespace-only lines so they do not become stray empty entries in the output.
  • Trim each line: removes leading and trailing whitespace per line, useful when the source list was copy-pasted from a table or chat message.

The tool reports the number of lines it processed, which is the easiest way to confirm that no input was silently dropped. Clean the source list first if it has duplicates, empties, or stray whitespace, then feed the cleaned output into Add Quotes to Each Line. Each layer should do one thing well.