在bash中切variable

from how split a string in bash?”

  1. Use # and % modifiers to ${}. I’ll do my example on colon instead of space:

    $ x=a:b:c
    $ echo ${x%:*}
    a:b
    $ echo ${x#*:}
    b:c

    The bit after the % or # is more like a shell glob than a regular expression, in case you want to get fancy there.

  2. #!/bin/bash

    thing="once upon a time"

    set -- $thing

    foo=$1
    shift
    bar=$@

    echo "foo: $foo"
    echo "bar: $bar"

    prints
    foo: once
    bar: upon a time