[kwlug-disc] fish vs bash: a progress bar from YSAP and some timing measurements

William Park opengeometry at yahoo.ca
Sat Aug 23 23:58:00 EDT 2025


For comparison, it would go like this in Bash...
You can't named argument in Bash.  Just positional arguments, $1, $2, 
$3, ...

myfunc()
{
     echo "Inside myfunc \$@ has $# items:"

     local -i i=1
     for arg; do
         echo " $i/$#: \$$i: ${!i} (or \$arg: $arg)"
         i+=1
     done
}

find -name '*.cache' -print0 | (
     readarray -d '' files
     echo "Found ${#files[@]) files in a test array"

     myfunc "${files[@]:0:5}"
)


On 2025-08-23 19:56, Ron wrote:
> Ron wrote on 2025-08-23 00:28:
> 
>> The fish syntax is *so much better*.
> 
> I'm reminded of how nginx came along and really took a lot of install 
> base from Apache.
> 
> Is it because it's faster? Maybe; debatable on equivalent work loads 
> (from my understanding).
> 
> 
> Is it because the configuration syntax is much cleaner, clearer, 
> legible, and sane?
> 
> I think so.
> 
> 
> 
> One quirk I've encountered is in passing an array to a function and 
> accessing it as a variable. This may be due to a failure to understand 
> something on my part, but I don't think so.
> 
> 
> Functions are defined something like this (formatting for personal 
> preference only):
> 
> ```
> #!/usr/bin/env fish
> 
> function myfunc                                            --description 
> "This appears in tab-completion hints"     -a my_arr
> 
>    echo -e "Inside myfunc \$my_arr has $(count $my_arr) item:
> $my_arr"
>    echo -e "Inside myfunc \$argv has $(count $argv) items:"
> 
>    set -l i 1
>    for arg in $argv
>      echo "  $i/$(count $argv): \$argv[$i]: $argv[$i] (or \$arg: $arg)"
>      set i (math $i + 1)
>    end
>    ## printf "myfunc argv passed: %s
> " $argv
> end
> 
> ## In bash, ** requires `shopt -s globstar`
> ## and `shopt -s nullglob` lest one gets either the string "./**/*.cache"
> ## if no matches or something else unwanted (I forget)
> set files (ls ./**/*.cache)
> echo "Found $(count $files) files in a test array"
> ## Indexing starts at 1 (yay!):
> myfunc $files[1..5]
> 
>   $ ./test2.fish
> Found 500 files in a test array
> Inside myfunc $my_arr has 1 item:
>   ./baz/delme-100.cache
> Inside myfunc $argv has 5 items:
>    1/5: $argv[1]: ./baz/delme-100.cache (or $arg: ./baz/delme-100.cache)
>    2/5: $argv[2]: ./baz/delme-101.cache (or $arg: ./baz/delme-101.cache)
>    3/5: $argv[3]: ./baz/delme-102.cache (or $arg: ./baz/delme-102.cache)
>    4/5: $argv[4]: ./baz/delme-103.cache (or $arg: ./baz/delme-103.cache)
>    5/5: $argv[5]: ./baz/delme-104.cache (or $arg: ./baz/delme-104.cache)
> ❰uid1❙~/utils/ysap-bash/progress-bar❱ ✔ ≻
> ```
> 
> So, passing in an array and capturing the entire array in a local 
> variable does not (seem to) work.
> 
> However, that array is accessible as $argv, so ... that's okay.
> 
> 
> I do like that indexing starts at 1 - that's great!
> 
> 
> _______________________________________________
> kwlug-disc mailing list
> To unsubscribe, send an email to kwlug-disc-leave at kwlug.org
> with the subject "unsubscribe", or email
> kwlug-disc-owner at kwlug.org to contact a human being.



More information about the kwlug-disc mailing list