Linux: Dynamically creating a command

Problem

You want to create a command dynamically in your script

Solution

#!/bin/bash

CMD="$(ls -t L*.log | head -n1)"
eval cp ${CMD} load.dat


The command would be whatever you want in the highlighted brackets above.
The eval command then puts it together. So in the above example I am building a cp command based on the latest log file in the current directory.
"cp L543435.log load.dat"

Acknowledgement

http://stackoverflow.com/questions/1715591/dynamically-building-a-command-in-bash

Comments