
ターミナルエミュレータは常にシェルを介して間接的にプログラムを実行しますか?
たとえば、ターミナルエミュレータウィンドウを開くと、自動的にシェルが実行され、シェルにはコマンドのみを入力できます。
たとえば、次のように端末エミュレータから直接プログラムを実行すると、
xterm -e "echo hello; sleep 5"
xterm
プログラムはシェルを介して間接的に実行されますか、それともシェルを使用せずに直接実行されますか?
答え1
端末エミュレータによって異なります。
xterm
execvp(2)
与えられた引数を使用して最初に呼び出されますが、失敗して引数が-e
続く場合も試みられます。command
-e
$SHELL -c command
mlterm
失敗するとrxvt
エラーが発生しますexecvp
。
2番目の段落に説得力がない場合は、次のことを試してください。
$ mkdir /tmp/tbin; ln -s /usr/bin/vi '/tmp/tbin/echo hello; sleep 5'
$ PATH=$PATH:/tmp/tbin xterm -e 'echo hello; sleep 5'
それとも見てください源泉。
答え2
あなたの例で適切な-e
オプションを使用すると、xterm
マニュアルに記載されているシェルが起動します。
シェルのxtermのデフォルト検索を無視することができるため、独自のプログラムを提供できますが、シェルを無視するときに-eオプションを使用することはできません。シェルを書き換えるとき、あなたの殻xterm( fork() + exec()
) で直接実行します。
関連部分は次のとおりです。
One parameter (after all options) may be given. That overrides xterm's built-in choice of
shell program. Normally xterm checks the SHELL variable. If that is not set, xterm tries
to use the shell program specified in the password file. If that is not set, xterm uses
/bin/sh. If the parameter is not a relative path, i.e., beginning with “./” or “../”, xterm
looks for the file in the user's PATH. In either case, it constructs an absolute path. The
-e option cannot be used with this parameter since it uses all parameters following the
option.
そして
-e program [ arguments ... ]
This option specifies the program (and its command line arguments) to be run in the
xterm window. It also sets the window title and icon name to be the basename of the
program being executed if neither -T nor -n are given on the command line. This
must be the last option on the command line.
あなたが実行しているものを見てください。
"echo hello; sleep 5"
シェルは文字列を解析し、PATH
env変数を使用して2つのコマンドを見つけ、実際にはセミコロンで区切られた2つのコマンドであることを認識しますが、そうでxterm
はありません!
答え3
マニュアルによれば、次のパラメータを使用してログインシェルを無効にできます+ls
。
+ls This option indicates that the shell that is started should not be a login shell (i.e., it will be a normal “subshell”).
だからxterm -e "echo hello"
シェルは生産されますが、xterm +ls -e "echo hello"
そうではありません。