Click here to Skip to main content
15,917,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys i have a function in bash , for me is correct but i dontknow why continue to tell me
./BBSW.sh: 17: Syntax error: Bad for loop variable

anyone can confirm me if is ok or not?
urlencode() {
    local string="$1"
    local length="${#string}"
    local result=""

    # Verifica se la sintassi del ciclo for è supportata
    if [ -z "$(bash -c 'for ((i=0; i<1; i++)); do echo "OK"; done' 2>/dev/null)" ]; then
        echo "Errore: La sintassi del ciclo for non è supportata in questa shell."
        return 1
    fi

    for ((i = 0; i < length; i++)); do
        local c="${string:$i:1}"
        case $c in
            [-_.~a-zA-Z0-9]) result+="$c" ;;
            *) result+="%$(printf '%02X' "'$c")" ;;
        esac
    done

    echo "$result"
}


my version is
 bash --version
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

i use linux mint 64 last version 


What I have tried:

i try also in this mode but nothing
urlencode() {
    local string="$1"
    local length="${#string}"
    local result=""

    for (( i = 0; i < length; i++ )); do
        local c="${string:$i:1}"
        case $c in
            [-_.~a-zA-Z0-9]) result+="$c" ;;
            *) result+="%$(printf '%02X' "'$c")" ;;
        esac
    done

    echo "$result"
}
Posted
Updated 23-Feb-24 9:59am
v2

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900