bashスクリプトから "x-terminal-emulator -e bash -c"にパラメータを渡すには?

bashスクリプトから

次のスクリプトがあるとしましょう。

x-terminal-emulator -e bash -c 'echo hello > ~/text'

名前をfoo.shとして指定し、実行可能にしました。

このスクリプトを実行すると、ホームフォルダに「hello」という単語を含むテキストファイルが生成されます。

今、私は次のように修正したら:

x-terminal-emulator -e bash -c 'echo $1 > ~/text'

...次のように端末で実行します。

./foo.sh hello

私のホームフォルダに内容がないテキストファイルがあります。

foo.shは最初と唯一のパラメータ($ 1)で「hello」を受け取ります。それでは、なぜbashはそれを受け取らないのでしょうか? foo.shからbashに1つ以上のパラメータを渡す方法はありますか?

変数名にパラメータを保存してエクスポートしようとしましたが、結果は変更されませんでした。

ベストアンサー1

~からman bash


   -c        If the -c option is present, then commands are read from the
             first  non-option  argument  command_string.   If  there are
             arguments after the command_string, they are assigned to the
             positional parameters, starting with $0.

だからあなたはできます

x-terminal-emulator -e bash -c 'echo $0 > ~/text' "$1"

または(パラメータの「一般的な」番号付けを維持したい場合)

x-terminal-emulator -e bash -c 'echo $1 > ~/text' _ "$1"

ここでは、_必要なダミー変数に置き換えることができます。

おすすめ記事