bash変数周りのメモ

シェル変数

||0|| プログラム名 ||1 ~ 9|| 第 1 ~9引数 ||@|| 全ての引数。"$@" としたときは “$1” “$2” “$3”… に等しい。 ||*|| 全ての引数。"$*" としたときは “$1 $2 $3…” に等しい。 ||#|| 引数の数 ||?|| 直前に実行したプログラムの終了コード。 ||RANDOM|| ランダムな数字。0~32767 が帰ってくる模様。 ||$|| プロセス ID

Geek なぺーじ:10 の UNIX 小技

変数と「"」の組み合わせは慎重に

特別な理由が無い限り、変数は「"」で囲う方が良いでしょう。
もし、変数名の直後に文字を使いたいのであれば、変数名を「{}」で囲いましょう。
これを忘れてしまうと、別の変数名として扱われてしまいます。
意図しない別の変数名になってしまった場合、大抵はnull値になるでしょう。

$ ls tmp/
a b
$ VAR="tmp/*"
$ echo $VAR
tmp/a tmp/b
$ echo "$VAR"
tmp/*
$ echo $VARa

$ echo "$VARa"

$ echo "${VAR}a"
tmp/*a
$ echo ${VAR}a
tmp/a
$

コマンドのグループ化

Geek なぺーじ:10 の UNIX 小技

コマンドをグループ分けする

大抵のシェルには、コマンドをグループ分けして実行する機能を持っています。
一般的には、subshellで行う方法と、現在実行しているshellで行う2通りの方法があります。

subshell

subshellを使う方法は、環境変数を定義しなおしたい場合に有効です。
subshellで環境変数を変更しても、現在実行中のshellが保持する環境変数は維持されます。

$ ( cd tmp/a/b/c/ || mkdir -p tmp/a/b/c && \
> VAR=$PWD; cd ~; tar xvf -C $VAR archive.tar ) \
> | mailx admin -S "Archive contents"

現在のshell

「{}」の前後にスペースが入るように注意しましょう。
スペースを入れないと正しく動作しないかも知れません。

また、「}」の前に来る最後のコマンドの後に「;」を入れるのを忘れないようにしましょう。

$ { cp ${VAR}a . && chown -R guest.guest a && \
> tar cvf newarchive.tar a; } | mailx admin -S "New archive"

23:54:18.660445 IP (tos 0x0, ttl 249, id 64056, offset 0, flags [DF], proto TCP (6), length 113)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xcd71 (correct), seq 4343:4404, ack 582, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 61

23:54:18.660811 IP (tos 0x0, ttl 249, id 64057, offset 0, flags [DF], proto TCP (6), length 87)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x30ba (correct), seq 4404:4439, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 35

23:54:18.661562 IP (tos 0x0, ttl 249, id 64058, offset 0, flags [DF], proto TCP (6), length 83)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x9550 (correct), seq 4439:4470, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 31

23:54:18.661955 IP (tos 0x0, ttl 249, id 64059, offset 0, flags [DF], proto TCP (6), length 279)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x8ab1 (correct), seq 4470:4697, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 227

23:54:18.662740 IP (tos 0x0, ttl 249, id 64060, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0xcc8a (correct), seq 4697:5925, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663309 IP (tos 0x0, ttl 249, id 64061, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x4776 (correct), seq 5925:7153, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663464 IP (tos 0x0, ttl 249, id 64062, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x76f1 (correct), seq 7153:8381, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663886 IP (tos 0x0, ttl 249, id 64063, offset 0, flags [DF], proto TCP (6), length 495)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xda23 (correct), seq 8381:8824, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 443

23:54:18.663905 IP (tos 0x0, ttl 249, id 64064, offset 0, flags [DF], proto TCP (6), length 2508)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xe7ea (incorrect -> 0xb83e), seq 8824:11280, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 2456

23:54:18.732033 IP (tos 0x0, ttl 249, id 64066, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x9f86 (correct), seq 11280:12508, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.660445 IP (tos 0x0, ttl 249, id 64056, offset 0, flags [DF], proto TCP (6), length 113)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xcd71 (correct), seq 4343:4404, ack 582, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 61

23:54:18.660811 IP (tos 0x0, ttl 249, id 64057, offset 0, flags [DF], proto TCP (6), length 87)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x30ba (correct), seq 4404:4439, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 35

23:54:18.661562 IP (tos 0x0, ttl 249, id 64058, offset 0, flags [DF], proto TCP (6), length 83)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x9550 (correct), seq 4439:4470, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 31

23:54:18.661955 IP (tos 0x0, ttl 249, id 64059, offset 0, flags [DF], proto TCP (6), length 279)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x8ab1 (correct), seq 4470:4697, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 227

23:54:18.662740 IP (tos 0x0, ttl 249, id 64060, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0xcc8a (correct), seq 4697:5925, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663309 IP (tos 0x0, ttl 249, id 64061, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x4776 (correct), seq 5925:7153, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663464 IP (tos 0x0, ttl 249, id 64062, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x76f1 (correct), seq 7153:8381, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663886 IP (tos 0x0, ttl 249, id 64063, offset 0, flags [DF], proto TCP (6), length 495)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xda23 (correct), seq 8381:8824, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 443

23:54:18.663905 IP (tos 0x0, ttl 249, id 64064, offset 0, flags [DF], proto TCP (6), length 2508)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xe7ea (incorrect -> 0xb83e), seq 8824:11280, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 2456

23:54:18.732033 IP (tos 0x0, ttl 249, id 64066, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x9f86 (correct), seq 11280:12508, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228