文字列のm文字目からn文字目までを取り出す(set --)
setコマンドは文字列を単語に分割するにも使える
最初の引数 -- はそれ以降のオプションの解釈をしないようにするためのオプション
文字列の部分は""でくくらないようにする
分割した結果は $1-$9 $* $@ で参照できる
setを使うと引数が上書きされるため、必要なら前もって退避させる
#!/bin/sh #str_wordsplit.sh text='You nature demands love and your happeness depends on it.' set -- $text echo "Number of words: $#" echo "The fourth word: $4"
$ ./str_wordsplit.sh Number of words: 10 The fourth word: love