我为此创建了两个实用程序脚本:
read-string.sh
从输入中读取字符串(需要用户按回车键)
read-char.sh
从输入中读取单个字符(不需要按回车键)
无论用户使用的是bash还是zsh ,这两个脚本都可以正常工作。
#!/bin/bash
# read-string.sh
# eg: my_string=$(./read-string.sh); echo "my_string: $my_string"
# bash `read` manual - https://ss64.com/bash/read.html
#
# read [-ers] [-a aname] [-d delim] [-i text] [-n nchars]
# [-N nchars] [-p prompt] [-r] [-s] [-t timeout] [-u fd]
# [name...]
#
# -r Do not treat a Backslash as an escape character. The backslash is considered to be part
# of the line. In particular, a backslash-newline pair can not be used as a line continuation.
# Without this option, any backslashes in the input will be discarded.
# You should almost always use the -r option with read.
# zsh `read` manual - http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html#index-read
#
# read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
# [ -u n ] [ name[?prompt] ] [ name ... ]
#
# -r Raw mode: a ‘\’ at the end of a line does not signify line continuation and backslashes in the line
# don’t quote the following character and are not removed.
if [ -n "$ZSH_VERSION" ]; then
read -r "answer?"
else
read -r answer
fi
echo "$answer"
#!/bin/bash
# eg: my_char=$(read-char.sh); echo "my_char: $my_char"
# bash `read` manual - https://ss64.com/bash/read.html
#
# read [-ers] [-a aname] [-d delim] [-i text] [-n nchars]
# [-N nchars] [-p prompt] [-r] [-s] [-t timeout] [-u fd]
# [name...]
#
# -r Do not treat a Backslash as an escape character. The backslash is considered to be part
# of the line. In particular, a backslash-newline pair can not be used as a line continuation.
# Without this option, any backslashes in the input will be discarded.
# You should almost always use the -r option with read.
# -n nchars read returns after reading nchars characters rather than waiting for a complete line of input.
# zsh `read` manual - http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html#index-read
#
# read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
# [ -u n ] [ name[?prompt] ] [ name ... ]
#
# -q Read only one character from the terminal and set name to ‘y’ if this character was ‘y’ or ‘Y’
# and to ‘n’ otherwise. With this flag set the return status is zero only if the character was ‘y’ or ‘Y’.
# This option may be used with a timeout (see -t); if the read times out, or encounters end of file,
# status 2 is returned. Input is read from the terminal unless one of -u or -p is present.
# This option may also be used within zle widgets.
# -r Raw mode: a ‘\’ at the end of a line does not signify line continuation and backslashes in the line
# don’t quote the following character and are not removed.
if [ -n "$ZSH_VERSION" ]; then
read -r -q "answer?"
else
read -r -n 1 answer
fi
echo "$answer"
感谢奥拉夫的原始回答。