Browse > Home / Archive: 10. April 2006

| Subcribe via RSS

BASH: Split a string without 'cut' or 'awk'

April 10th, 2006 | 27 Comments | Posted in Bash

For a little test script I'm writing I needed to split a line on a ';' but preservere the "s and 's, something that echo doesn't like to do. Digging deeper into the bash docs I see that there are some handy string handling functions.

#!/bin/bash
line='this "is" a command;this "is" a pattern'
COMMAND=${line%;*}
PATTERN=${line#*;}
echo $COMMAND
echo $PATTERN

And the output would be:

this "is" a command
this "is" a pattern