from how split a string in bash?”
- 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:cThe bit after the % or # is more like a shell glob than a regular expression, in case you want to get fancy there.
-
#!/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