13,191
回編集
(→引用符) |
|||
| 175行目: | 175行目: | ||
a | a | ||
b | b | ||
</syntaxhighlight> | |||
<br> | |||
パラメータ展開や文字エスケープの機能を無視する場合、<code>'</code>または<code>"</code>を使用する。<br> | |||
<code>'</code>の間では、Fishは展開を行わない。<br> | |||
<code>"</code>の間では、Fishは変数の展開のみを行う。<br> | |||
<br> | |||
他の種類の展開(波括弧の展開やパラメータの展開を含む)は行われず、エスケープシーケンス(例. <code>\n</code>)は無視される。<br> | |||
引用符の中では、引数の区切りに空白を使用しないため、引用符で囲まれた引数に空白を含めることができる。<br> | |||
<br> | |||
<code>'</code>をエスケープする場合は<code>\'</code>、<code>"</code>をエスケープする場合は<code>\"</code>、ドル文字をエスケープする場合は<code>\$</code>、バックスラッシュと改行を削除する場合は<code>\</code>、バックスラッシュ記号をエスケープする場合は<code>\\</code>である。<br> | |||
<br> | |||
<code>'</code>は<code>"</code>内では特別な意味を持たないが、その逆も同様である。<br> | |||
<syntaxhighlight lang="fish"> | |||
# "cumbersome filename.txt"という名前のファイルを削除する | |||
rm "cumbersome filename.txt" | |||
# "cumbersome"ファイルと"filename.txt"ファイルの2つのファイルを削除する | |||
rm cumbersome filename.txt | |||
# foo.txtファイルの内容から、enabled)で終わる行を検索する | |||
# (grepコマンドにおいて、$記号は行末にマッチする) | |||
grep 'enabled)$' foo.txt | |||
</syntaxhighlight> | |||
<br><br> | |||
== 文字列の操作 == | |||
Fishの文字列の操作は、<code>string</code>コマンド(組み込み関数)により行われる。<br> | |||
<br> | |||
"bar "を"baz "に置換する。<br> | |||
<syntaxhighlight lang="fish"> | |||
string replace bar baz "bar luhrmann" | |||
# 出力 | |||
baz luhrmann | |||
</syntaxhighlight> | |||
<br> | |||
文字列を分割する。<br> | |||
<syntaxhighlight lang="fish"> | |||
string split "," "foo,bar" | |||
# 出力 | |||
foo | |||
bar | |||
</syntaxhighlight> | |||
<br> | |||
<code>grep</code>コマンドの代替として、正規表現にマッチする。<br> | |||
<syntaxhighlight lang="fish"> | |||
echo bababa | string match -r 'aba$' | |||
# 出力 | |||
aba | |||
</syntaxhighlight> | |||
<br> | |||
文字列を任意の文字で、指定された幅になるように埋める。<br> | |||
<syntaxhighlight lang="fish"> | |||
string pad -c x -w 20 "foo" | |||
# 出力 | |||
xxxxxxxxxxxxxxxxxfoo | |||
</syntaxhighlight> | |||
<br> | |||
文字列を小文字または大文字にする。<br> | |||
<syntaxhighlight lang="fish"> | |||
# 小文字に変換する | |||
string lower Foo | |||
# 出力 | |||
foo | |||
# 大文字に変換する | |||
string upper Foo | |||
# 出力 | |||
FOO | |||
</syntaxhighlight> | |||
<br> | |||
その他、文字列のリピート、文字列のトリム、文字列のエスケープ、文字列の長さや幅の表示(端末セル単位)を行うこともできる。<br> | |||
<br><br> | |||
== 特殊な変数 == | |||
下表に、FishとBashの特殊な変数の比較を示す。<br> | |||
<center> | |||
{| class="wikitable" | style="background-color:#fefefe;" | |||
|- | |||
! style="background-color:#66CCFF;" | Bash | |||
! style="background-color:#66CCFF;" | Fish | |||
|- | |||
| $*<br>$@ || $argv | |||
|- | |||
| $1<br>$2 || $argv[1]<br>$argv[2] | |||
|- | |||
| $? || $status | |||
|- | |||
| $$ || $fish_pid | |||
|- | |||
| $# || Fishには存在しないため、<br><code>count $argv</code>コマンドを使用する。 | |||
|- | |||
| $! || $last_pid | |||
|- | |||
| $0 || Fishには存在しないため、<br><code>status filename</code>コマンドを使用する。 | |||
|- | |||
| $- || Fishには存在しないため、<br><code>status is-interactive</code>または<code>status is-login</code>を使用する。 | |||
|} | |||
</center> | |||
<br><br> | |||
== ヒアドキュメント == | |||
Fishは、<code><<EOF</code>文は存在しないため、<code>printf</code>文、または、<code>""</code>と改行を使用して、ヒアドキュメントを行う。<br> | |||
<br> | |||
<syntaxhighlight lang="fish"> | |||
printf %s\n "some string" "some more string" | |||
# または | |||
echo "some string | |||
some more string" | |||
# または、引用符を別の行にする場合 | |||
echo "\ | |||
some string | |||
some more string\ | |||
" | |||
</syntaxhighlight> | |||
<br> | |||
ヒアドキュメントが行うことを、以下に示す。<br> | |||
* 文字列を特殊なルールで終端まで読み取り、解釈する。 | |||
* 結果の文字列を一時ファイルに書き出す。 | |||
* そのファイルを標準入力として、ヒアドキュメントが添付されているコマンドを起動する。 | |||
<br> | |||
ヒアドキュメントと同様に、コマンドはstdinから読み込むように準備しなければならない。<br> | |||
これには特別なオプションが必要なこともあり、多くの場合、ファイル名を<code>-</code>で与えることにより有効になる。<br> | |||
<syntaxhighlight lang="fish"> | |||
echo "xterm | |||
rxvt-unicode" | sudo zypper remove - | |||
# 以下に示すコマンドと同様である | |||
# -は、zypperが標準入力から引数を読み込むようにしている | |||
sudo zypper remove xterm rxvt-unicode | |||
</syntaxhighlight> | |||
<br> | |||
ヒアドキュメントは、多くの特殊なルールを導入するマイナーな構文上の味付けに過ぎないため、Fishではパイプを使用することにより、簡潔に記述・構成することができる。<br> | |||
<br><br> | |||
== 算術演算 == | |||
Fishでは、算術演算は<code>math</code>コマンドにより処理される。<br> | |||
<syntaxhighlight lang="fish"> | |||
math $i + 1 | |||
</syntaxhighlight> | |||
<br> | |||
他のシェルの算術演算と異なり、Fishでは浮動小数点数を扱うことができる。<br> | |||
<syntaxhighlight lang="fish"> | |||
math 5 / 2 | |||
# 出力 | |||
2.5 | |||
</syntaxhighlight> | |||
<br> | |||
三角測量のような機能も存在する。<br> | |||
<syntaxhighlight lang="fish"> | |||
math cos 2 x pi | |||
# 出力 | |||
1 | |||
</syntaxhighlight> | |||
<br> | |||
<code>math</code>コマンドの引数は、別々に渡す、または、引用符で囲んで渡すことができる。<br> | |||
<u>Fishは、コマンド実行に<code>()</code>を使用するため、式の中で使用する場合は、引用符で囲む必要がある。</u><br> | |||
<syntaxhighlight lang="fish"> | |||
math '(5 + 2) * 4' | |||
</syntaxhighlight> | |||
<br> | |||
<code>*</code>と</code>x</code>は両方ともFishにおける乗算記号であるが、<code>*</code>を使用する場合は、引用符で囲む必要がある。(グロブに見えるため)<br> | |||
<br><br> | |||
== プロンプト == | |||
Fishは、<code>$PS1</code>や<code>$PS2</code>等の特殊変数は存在しない。<br> | |||
<code>fish_prompt</code>関数の出力、または、vi-modeが有効な場合は<code>fish_mode_prompt</code>関数、右プロンプトの場合は<code>fish_right_prompt</code>関数を加えたものが使用できる。<br> | |||
<br> | |||
* テキストに色を付けるため、<code>set_color</code>を提供している。<br>これは、16色の名前付きカラーやRGBカラー(例. <code>set_color 5555FF</code>)を使用することができる。 | |||
* プロンプトに何かを追加するためのヘルパー関数が存在する。 | |||
*: 例1. バージョン管理システム(git、mercurial、svn)の表示を追加する<code>fish_vcs_prompt</code>関数 | |||
*: 例2. 現在の場所(ユーザのホームディレクトリは<code>~</code>になり、パスの構成要素が全て短縮される)を表示する<code>prompt_pwd</code>関数 | |||
*: 例3. ホスト名を短縮して表示する<code>prompt_hostname</code>関数 | |||
<syntaxhighlight lang="fish"> | |||
function fish_prompt | |||
set -l prompt_symbol '$' | |||
fish_is_root_user; and set prompt_symbol '#' | |||
echo -s (prompt_hostname) \ | |||
(set_color blue) (prompt_pwd) \ | |||
(set_color yellow) $prompt_symbol (set_color normal) | |||
end | |||
</syntaxhighlight> | |||
<br> | |||
デフォルトのプロンプトを確認する場合は、<code>fish_prompt</code>コマンドを実行することにより、表示することができる。<br> | |||
<br> | |||
Fishは、継続行のための<code>$PS2</code>は存在しない。<br> | |||
コマンドラインがまだ完了していないことを示すため、行をインデントしたままにしている。<br> | |||
<br><br> | |||
== ブロック == | |||
FishとBashのブロックの構成を比較する。<br> | |||
<syntaxhighlight lang="bash"> | |||
# Bash | |||
{ | |||
echo Hello | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="fish"> | |||
# Fish | |||
begin | |||
echo Hello | |||
end | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | <br><br> | ||