LoginSignup
8
7

More than 3 years have passed since last update.

2017-04-28 bash > 前のコマンドの引数を指定して使う > !:2 > 直前のコマンドの2つ目の引数 / !998:2 > 過去のコマンド(998番目)の2つ目の引数

Last updated at Posted at 2017-04-28
動作環境
Xeon E5-2620 v4 (8コア) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 とその-devel
mpich.x86_64 3.1-5.el6とその-devel
gcc version 4.4.7 (とgfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.1を使用。
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv

直前のコマンドの引数を取る

bashにて長い引数を使った処理をするとする。
次のコマンドで、同じ引数の文字列を別の引数位置で使いたい場合にどうするか調べた。

!:2のように書くと「前のコマンドの2つ目の引数」を得ることができる
( 複数の引数を取る方法も記載されている )

$ echo 3.1415926535 6.022 10^23
3.1415926535 6.022 10^23
$ echo !:2
echo 6.022
6.022
$ echo !:2 !:3
bash: :2: bad word specifier

「直前の」コマンドの引数を使うという点では問題がない。

実際に使う場面として

  1. テンプレートファイルをPREFIXをつけてコピー
  2. コピーしたファイルをシンボリックリンク
  3. そのファイルを編集
  4. 処理開始
$ cp namelist.input namelist.input.test170428
$ ln -fs !:2 LN-target
ln -fs namelist.input.test170428 LN-target
$ ls -l LN-target 
lrwxrwxrwx 1 xxx xxx 25 Apr 28 12:05 LN-target -> namelist.input.test170428

過去のコマンドの引数を取る

(追記 2017/05/11)

$history
...
  997  history 
  998  history | grep mpirun
...
$ echo !998:2
echo grep
grep
$ echo !998:3
echo mpirun
mpirun

上記の方法で、過去のコマンドの引数を取れることに気づいた。

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7