Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern matching in bash with tuple-like arguments

Tags:

bash

I want to pass a variable number of 'tuples' as arguments into a bash script and go through them in a loop using pattern matching, something like this:

for *,* in "$@"; do
    #do something with first part of tuple
    #do something with second part of tuple
done

is this possible? If so, how do I access each part of the tuple?

For example I would like to call my script like:

bash bashscript.sh first_file.xls,1 second_file,2 third_file,2 ... nth_file,1

like image 462
azrosen92 Avatar asked Sep 12 '25 19:09

azrosen92


1 Answers

Since bash doesn't have a tuple datatype (it just has strings), you need would need to encode and decode them yourself. For example:

$ bash bashscript.sh first_file.xls,1 second_file,2 third_file,2 ... nth_file,1

In bashscript.sh:

for tuple in "$@"; do
    IFS=, read first second <<< "$tuple"
    ...
 done
like image 144
chepner Avatar answered Sep 15 '25 12:09

chepner