copy and destroy

catch and eat

日記の練習です。

ただ bash のプロンプトをシンプルにしたいだけなのに、コマンド展開とかブレース展開とかシェルスクリプト(というか関数)とか変数の話になっている。

https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x834.html

#!/bin/bash
#   Dan's prompt looks like this:
#      543,pts/3,0$
#
function dan {
        local cur_tty=$(temp=$(tty) ; echo ${temp:5});
        PS1="\!,$cur_tty,\$?\$ "
}

${temp:5} がブレース展開で、変数 temp に入っている tty の出力 "/dev/pts/0" という文字列(11文字)を先頭から5文字分のオフセットで出力 "pts/0" している(なにを言っているのか)。

ブレース展開はインラインでも使えそう。テスト

774,pts/2,0$ touch foobar.c
775,pts/2,0$ ls
foobar.c 
776,pts/2,0$ mv -v foobar{,.0}.c
renamed 'foobar.c' -> 'foobar.0.c'
777,pts/2,0$ ls
foobar.0.c

ブレース展開、履歴には展開されずに入力がそのまま残る

780,pts/2,0$ history 5
  776  mv -v foobar{,.0}.c
  777  ls
  778  pwd
  779  man rm
  780  history 5

powered by hatena blog.
the nikki system for lifelogging junkies.

all posts © their original owners.
writing is reusable solely under the by creative commons license.