シェルスクリプト - Linux

最終更新日: 2026-08-29

TOP(About this memo)) > 一覧(Linux) > シェルスクリプト

基本

#!/bin/bash
chmod 755 test.sh
./test.sh
# もしくは
sh test.sh
bash test.sh

終了コード

some-command --project=$A --platform=$B
retval=$?
if [ $retval -ne 0 ]; then
    echo "コマンドの実行に失敗しました。" >&2
    exit 1
fi

引数チェック

if [ $# -ne 3 ]; then
    echo "引数が足りません。" >&2
    exit 1
fi

ファイル存在チェック

if [ -e "path/to/file.plist" ]; then
    rm path/to/file.plist
fi

パラメータ展開

.bash_profile の例

export SOME_HOME=/path/to/somewhere
alias la='ls -la'
alias ll='ls -la'

sh -c / bash -c の注意