Redirect bash output to dynamic file name Redirect bash output to dynamic file name bash bash

Redirect bash output to dynamic file name


Just concatenate $1 with the "team.csv".

#!/bin/bashmdb-export 2011ROXBURY.mdb TEAM > "${1}team.csv"

In the case that they do not pass an argument to the script, it will write to "team.csv"


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