文字列の代わりに名前に関数パラメータを渡す

文字列の代わりに名前に関数パラメータを渡す

この機能を使用すると:

repr() {
    declare -p $1 | cut -d '=' -f 2- > /tmp/.repr
    $1=$(</tmp/.repr)
    rm /tmp/.repr
}

書くと、次のエラーメッセージが表示されます。

repr test

これはパラメータを文字列として扱います。

repr() {
    declare -p 'test' | cut -d '=' -f 2- > /tmp/.repr
    'test'=$(</tmp/.repr)
    rm /tmp/.repr
}

そして名前が好きではありません。

repr() {
    declare -p test | cut -d '=' -f 2- > /tmp/.repr
    test=$(</tmp/.repr)
    rm /tmp/.repr
}

この問題をどのように解決できますか?

ベストアンサー1

おすすめ記事