1. 指令:read

read 用來讓 script 暫停,等待使用者輸入,然後把輸入值存進變數。

echo "Enter your skill:"
read SKILL
echo "Your skill is $SKILL"

執行到 read 時,bash 會停下來等使用者輸入,按下 Enter 後繼續。


2. p 選項:提示輸入

可以在同一行加上提示字:

read -p "Enter your skill: " SKILL
echo "Your skill is $SKILL"

這樣不需要額外的 echo。


3. s 選項:隱藏輸入

常用於輸入密碼等機密資訊。

#!/bin/bash

echo "Enter your skills:"
read SKILL

echo "Your $SKILL skill is in high Demand in the IT Industry."

read -p 'Username: ' USR
read -sp 'Password: ' pass

echo

echo "Login Successfull: Welcome USER $USR,"

image.png

echo 是為了在輸完密碼後換行。


4. DevOps 中的實務建議

在自動化或 DevOps 環境中,不建議讓 script 互動式要求輸入,因為: