コマンドの一部に他のコマンドを埋め込んでバッククォート(`)で囲っておくと、その部分のコマンドが実行された結果が、その部分に置き換えられて残りのコマンドが実行される。
この機能は、コマンドの標準出力を変数に代入したり、他のコマンドの一部として再利用したい時に用いられる。
# pwd /CG/CGtec/abe # set dir = `pwd` # echo $dir /CG/CGtec/abe # ls test1.c test2.c # set files = `ls` <- 出力結果にスペースが含まれている時は配列になる # echo $files test1.c test2.c # echo $files[1] test1.c # echo $files[2] test2.c
# whoami <- 自分のログイン名を表示 au990000 # set prompt = `whoami` <- set prompt = au990000 と同じ au990000 <- プロンプトがau990000に変わる
# grep -l scanf *.c <- scanfが含まれているファイル名を表示 test1.c kadai2.c # vi `grep -l scanf *.c` <- vi test1.c kadai2.c と同じ( vi `!!` と実行しても良い )
# grep -l printf `grep -l scanf *.c` <- printf と scanf のふたつの文字列が含まれるファイル名を表示 test1.c
# csh grep2.sh printf scanf <- printf と scanf のふたつの文字列が含まれるファイル名を表示 test1.cgrep2.sh の内容
grep -l $argv[1] `grep -l $argv[2] *.c`