This Bash script executes each file in the files array on the database. However, it creates a new sqlcmd session for each file. Is there a way to create a single sqlcmd session before the loop and then use that session for all file executions?


# declare and initialize an array of files to be executed (listed in sequence of execution)
declare -a files=(

# loop through files array to execute each file on database
for file in "${files[@]}"
   sqlcmd -S SQLDEV14 -E -d D7stage -i ".\Modules\\$file" -m 1

If so, what is the syntax? I have searched blogs and forums for how to do this but have not found anything that's helpful.


2 Answers 2


I ended up doing this:

# loop through files array and build a string of comma separated input files in variable called inputFiles
for file in "${files[@]}"
# echo $inputFiles

# execute multiple input files on database using one sqlcmd session
sqlcmd -S SQLDEV14 -E -d D7stage -i $inputFiles -m 1

If you're using a shell such as bash you could concatenate the files and pipe them in as a single entity:

sqlcmd -S SQLDEV14 -E -d D7stage -i <(cd Modules && cat "${files[@]}") -m 1
  • This looks promising. How do I prepend the ".\Modules\\" for each file though?
    – knot22
    Commented Nov 23, 2022 at 20:32
  • @knot22 You can use bash parameter expansion, like this: <(cat "${files[@]/#/.\\Modules\\\\}") Commented Nov 23, 2022 at 20:49
  • @knot22 there you go Commented Nov 23, 2022 at 22:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .