-   Linux - Newbie (
-   -   Assign command output to a varible. (

wilku 03-26-2005 11:06 AM

Assign command output to a varible.

I'm trying to write a bash script that would dump mysql database (script run by cron once a week). I need to name the dump file with a current date like twilk_20050326.sql. I come to a problem at the very beginning trying to create filename varible:

FILE_NAME=date +twilk_%G%m%d.sql
This one gives an error of command not found: +twilk_%G%m%d.sql
I also tried without a space before "+", with single quotes, double quotes around "date ... .sql". All of them assign value of a string, not executing command.

Thanks for any help

wapcaplet 03-26-2005 11:08 AM

Try this:


FILE_NAME=$( date +twilk_%G%m%d.sql )
or this:


FILE_NAME=`date +twilk_%G%m%d.sql`
Both of those execute the command, and assign the command's output to FILE_NAME.

wilku 03-26-2005 11:13 AM

The first solution works. Thanks!

The second one, with single quotes, doesn't (at least for me). It assignes string value instead of executing command.

wapcaplet 03-26-2005 12:56 PM

Those are backquotes in the second example (the key next to the "1" on most U.S. keyboards). They serve the same purpose as the first example; the first one is bash-specific, though, so the backquotes one may be more portable to other scripting environments (if you're concerned at all about that). I prefer the first way, since it's easier to read.

All times are GMT -5. The time now is 01:36 AM.