直接執行 ./script.sh 不帶參數,或在腳本內部誤以為 $1 自動包含文件路徑。
sh your_script.sh /absolute/path/to/file
# 或
./your_script.sh /absolute/path/to/file
#!/bin/bash
filepath="$1" # 獲取第一個參數
if [[ -z "$filepath" ]]; then
echo "Usage: $0 "
exit 1
fi
cat "$filepath" # 使用雙引號防止路徑含空格
sh yourfile.sh 文件絕對路徑
eg: sh yourfile.sh /etc/