4

私はZSHにこのテスト変数を持っています:

test_str='echo "a \" b c"'

これを2つの文字列の配列に解析したいと思います("echo" "a \" b c")

つまり、シェル自体と同じように読み取りtest_str、引数の配列を返します。

私は空白などで分割するつもりはないことに注意してください。これは実際には、任意に複雑な文字列をシェル引数に解析することです。

4

2 に答える 2

8
于 2012-12-31T09:13:58.773 に答える
5

A simpler (?) answer is hinted at by the wording of the question. To set shell argument, use set:

#!/bin/sh

test_str='echo "a \" b"'
eval set $test_str
for i; do echo $i; done

This sets $1 to echo and $2 to a " b. eval certainly has risks, but this is portable sh. It does not assign to an array, of course, but you can use $@ in the normal way.

于 2013-01-01T13:39:28.817 に答える