why I can’t pass my input in foo function

foo() {
	read -r -p "delete $name (default is no) [y/n]?  " choice
	choice="${choice:-n}"
	echo "\$choice: $choice"
}

printf "%s\n" "foo" "bar" "baz" "eggs" "spam" | while read -r name; do
	foo

Expected result:

delete foo (default is no) [y/n]? USER_INPUT
$choice: USER_INPUT
delete bar (default is no) [y/n]? USER_INPUT
$choice: USER_INPUT
# truncated

Actual output:

$choice: bar
$choice: eggs
$choice: n

[!NOTE] Actual output produced without user interaction

  • Remus86@lemmy.zip
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    4 个月前

    Do this instead to treat name as a locally scoped variable:

    foo() {
        local name="$1"
        read -r -p "delete $name (default is no) [y/n]? " choice
        choice="${choice:-n}"
        echo "\$choice: $choice"
    }
    
    printf "%s\n" "foo" "bar" "baz" "eggs" "spam" | while read -r name; do
        foo "$name"
    done