Splitting a Command Line Like a Shell

medium
Unit 15 · Parsing I0/2 to milestone

Implement shell_split(line) (like shlex.split, without using it) that splits a command line into arguments:

  • whitespace separates arguments
  • inside single quotes everything is literal
  • inside double quotes, a backslash escapes " and \ (other backslashes stay as-is)
  • outside quotes, a backslash escapes the next character
  • quotes can be glued to other text: a"b c"d is one argument ab cd; "" is an empty argument
  • an unclosed quote or a trailing backslash raises ValueError
Pro problem

Unlock this and every premium problem across all tracks — plus hidden-test grading, advanced AI engineering modules and new content every week.

Start 7-day free trial
solution.py · Python 3.13
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Splitting a Command Line Like a Shell · TechGradify