Redirect bash output to dynamic file name
You can refer to a positional argument to a shell script via $1
or something similar. I wrote the following little test script to demonstrate how it is done:
$ cat buildcsv #!/bin/bashecho foo > $1.csv$ ./buildcsv roxbury$ ./buildcsv sarnold$ ls -l roxbury.csv sarnold.csv -rw-rw-r-- 1 sarnold sarnold 4 May 12 17:32 roxbury.csv-rw-rw-r-- 1 sarnold sarnold 4 May 12 17:32 sarnold.csv
Try replacing team.csv
with $1.csv
.
Note that running the script without an argument will then make an empty file named .csv
. If you want to handle that, you'll have to count the number of arguments using $#
. I hacked that together too:
$ cat buildcsv #!/bin/bash(( $# != 1 )) && echo Need an argument && exit 1echo foo > $1.csv