最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Bash git-hook script: read command skipped without user input - Stack Overflow

programmeradmin0浏览0评论

I'm trying to use a pre-push Git hook that prompts the user for confirmation before proceeding. However, when executed as a Git hook, the read command does not wait for user input and immediately proceeds as if an empty response was given.

Here's a minimal script reproducing the issue (.git/hooks/pre-push):

#!/usr/bin/sh

echo "Do you want to continue (o/n) ?"
read -r response
if [[ "$response" =~ ^[oO]$ ]]; then
    echo "Continue ...."
else
    echo "Stop !!!!"
    exit 1
fi
exit 0

Expected Behavior

  • The script should wait for user input before proceeding.

Actual Behavior

  • The script does not wait for input when executed as a Git hook and immediately treats the response as empty, leading to the "Stop !!!!" message.

What I've Tried

  • Running the script manually works fine (it waits for input).
  • Using /bin/bash instead of /usr/bin/sh.
  • Running read -p directly.
  • Redirecting input with exec < /dev/tty, but the behavior remains the same in the hook context.

Question

  • Why does read get skipped when executed inside a Git hook?
  • How can I properly prompt for user input in a Git hook?

Thanks for any insights!

发布评论

评论列表(0)

  1. 暂无评论