LoginSignup
8
12

More than 5 years have passed since last update.

文字列をタブ区切りで分割するけど、スペース分割はしたくないとき

Posted at

シェルでファイルから読み込んだ文字列をタブ区切りのみで分割したいときに、IFSの指定でハマったのでメモ。

tabSample.sh
#!/bin/sh

#お試し文字列
tData="tab1 tab2    tab3 space1
return"
echo $tData

#IFSのデフォルトは、スペース、タブ、改行。

#IFSへタブのみ設定する。
#タブを打ち込めばOK。
#スペース、改行でも区切られないようになる。
IFS='   '
#\t指定だとNGだった。
#IFS=$'\t'

#IFS確認用
echo -n "$IFS" | od -b

set -- $tData
echo $1
echo $2
echo $3

#処理が終わったらIFS設定をデフォルトへ戻しておく。
IFS=$' \t\n'
#IFS確認用
echo -n "$IFS" | od -b





result.txt

tab1 tab2 tab3 space1
0000000 011
0000001
tab1
tab2
tab3 space1
return
0000000 044 040 011 012
0000004

8
12
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
12