in reply to use variable as name for new file

Yeah. Use the $! var.

$! will only be set if a system call fails.

Your syntax is fine. You probably want to look at file permissions.