use strict; use warnings; my $str = 'command1 command2 command3 "command4 --some-arg arg --some-other-arg 2" command5'; for (split m{(\s+|"[^"]*")}, $str) { next if m/^\s/; next unless length $_; print "<$_>\n"; }