クリップボードにコピーしました!
文字列のcat

[cat] 実行して理解 ヒアドキュメントで変数の文字列を出力

created-2025/12/12 updated-2026/03/15

前書き

このリポジトリは、catコマンドを実行してヒアドキュメントに記述した変数を展開してファイルに書き込み出力します

インストールする必要のあるコマンド

  1. 特に無し

クイックスタート

catコマンドを実行してファイルに出力された文字列を出力してみてください

ubuntu

hello="hello here document world"
cat << EOF > test.txt
$hello
EOF
cat test.txt
出力結果
hello here document world

macos

MacPorts

hello="hello here document world"
cat << EOF > test.txt
$hello
EOF
cat test.txt
出力結果
hello here document world

windows

WSL2

hello="hello here document world"
cat << EOF > test.txt
$hello
EOF
cat test.txt
出力結果
hello here document world

MSYS2 MINGW64

hello="hello here document world"
cat << EOF > test.txt
$hello
EOF
cat test.txt
出力結果
hello here document world

実行手順

ubuntu

クリックして詳細を開く

事前確認

以下のコマンドを端末に打ち込んでcommand not foundが出なければokです

特にありません

preinstall

端末にcommand not foundが出たコマンドを以下のコマンドでインストールしてください

特にありません

コマンド

以下のコマンドを端末で実行することで詳細のような文字列が出力されるはずです

hello="hello here document world"
cat << EOF > test.txt
$hello
EOF
cat test.txt
詳細
hello here document world

macos

クリックして詳細を開く

事前確認

以下のコマンドをターミナルに打ち込んでcommand not foundが出なければokです

特にありません

※macosはMacPortsパッケージマネージャを使用してコマンドを管理します。もしインストールしていない方は以下のリンクからMacPortsのインストール手順をご覧ください
またコマンドに別名を設定して既存の環境と競合しないでコマンドを呼び出せるようにします。
初めてこのブログを利用する方は、以下の2つの記事を参考に環境構築してください

preinstall

ターミナルでcommand not foundが出たコマンドを以下のコマンドでインストールしてエイリアスを設定してください

特にありません

コマンド

以下のコマンドをターミナルで実行することで詳細のような文字列が出力されるはずです

hello="hello here document world"
cat << EOF > test.txt
$hello
EOF
cat test.txt
詳細
hello here document world

windows

クリックして詳細を開く

事前確認

以下のコマンドをプロンプトに打ち込んでcommand not foundが出なければokです

WSL2
特にありません
MSYS2 MINGW64
特にありません

※windowsはWSL2とMSYS2 MINGW64で確認しています。可能な限りWSL2をインストールしていただいて、もし設定できなかった場合はMSYS2をインストールすることで実行できますが、所々WSL2でしか実行できないコマンドが出てくるかもしれません。WSL2とMSYS2のインストール方法は下記の記事を参考にしてください

preinstall

プロンプトでcommand not foundが出たコマンドを以下のコマンドでインストールしてください

WSL2
特にありません
MSYS2 MINGW64
特にありません

コマンド

以下のコマンドをプロンプトで実行することで詳細のような文字列が出力されるはずです

WSL2
hello="hello here document world"
cat << EOF > test.txt
$hello
EOF
cat test.txt
詳細
hello here document world
MSYS2 MINGW64
hello="hello here document world"
cat << EOF > test.txt
$hello
EOF
cat test.txt
詳細
hello here document world

後書き

cat << EOF > test.txtは、次の行からEOFが現れるまでの文字列をcatコマンドで出力して、結果を>の右側に記述されたファイルに出力します
EOFは任意の文字列を指定できるので試しに以下のコードを入力してみてください

cat << aaa > test2.txt
test file
aaa
cat test2.txt

test fileという文字列が出力されるはずです

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

©︎ 2025-2026 todo