Skip to content
Advertisement

Using a bash script to dynamically generate another bash script and run it

Is there a way to generate a Bashscript dynamically using a Bashscript, change the mod of the generated Bashscript to allow execution, and then run the generated Bashscript?

Advertisement

Answer

yes it is totally feasible!

#!/bin/bash

FILE="/tmp/generated.bash"

cat <<END > $FILE
#!/bin/bash

echo "Hello There from generated script!" 
END

chmod 755 $FILE
echo "running ...."
$FILE
echo "done"

gonna give you the output like :

[tmp]=> ./test.bash
running ....
Hello There from generated script!
done
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement