In bash, you can use an array to keep the selected arguments. Use [[ ... == ... ]]
with parameter matching to find the digits.
#! /bin/bash
contain_digit=()
for arg ; do
shift
if [[ "$arg" == *[0-9]* ]] ; then
contain_digit+=("$arg")
fi
done
printf '%s ' "${contain_digit[@]}"
echo
If you want to go the grep
way, use printf
to output each argument on a separate line (doesn't work for multiline arguments).
printf '%s
' "$@" | grep '[0-9]'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…