bashbrace-expansion

Bash how to feed multiple command line arguments to Xgraph


Using the Xgraph data plotting tool (XGRAPH), I want to plot a bunch of files named test1.csv test2.csv, ...

The command would be

xgraph -columns 1 2 test1.csv -columns 1 2 test2.csv

and so on· Is there any way I can use brace expansion or some other magic to circumvent manually typing the pattern -columns 1 2 file.csv again and again?


Solution

  • I think this function should work for you...

    graph-build() {
      # loop through commandline filenames and join together
      for file in "${@}"; do
        cmd_build+=(-columns 1 2 "${file}")
      done
      # run xgraph
      xgraph "${cmd_build[@]}"
    }
    
    # Run with the expansion as required.
    graph-build file{1..3}.csv
    

    Edit: Syntax updated due to helpful comments highlighting the whitespace issue with filenames in my initial revisions.