Blake Regalia
Blake Regalia

Reputation: 2766

Redirect output to filename given by result of command

Suppose I have a file format I want to save database backups to given as such:

echo "~/backups/$(date +'%Y-%m-%d_%H-%M-%S').sql"

Now how can I specify this result as a filename for output in shell?

mysqldump my_db > ....?

By the way: shell interprets the result of a nested echo command as an executable command/file. So    It would seem that:

 mysqldump my_db > $(echo "something")

does NOT work. Instead, shell looks for a file called something and tries executing it?

Upvotes: 2

Views: 1709

Answers (3)

Mohit Rathore
Mohit Rathore

Reputation: 428

I think there are more possibilities to answer this question: How to name a file with current time? [duplicate]

--

You can create a file in bash by using different utilities

Using touch

root@ubuntu:~/T/e/s/t# touch $(date +"%T") //you can format time as per your need 

Using editor

root@ubuntu:~/T/e/s/t# vi $(date +"%T") 

Using redirection

root@ubuntu:~/T/e/s/t# echo "Input for file" > $(date +"%T")

Time format

In place of %T you can use your own time format Refer: http://man7.org/linux/man-pages/man1/time.1.html

Upvotes: -1

Batcher
Batcher

Reputation: 167

$(echo "something") is not the problem, while ~ is. It works fine if you use full path:

echo 'hello world' > $(echo "/home/root/backups/$(date +'%Y-%m-%d_%H-%M-%S').sql")

In case you're interested in how to use ~:

eval "echo 'hello world' > $(echo "~/backups/$(date +'%Y-%m-%d_%H-%M-%S').sql")"

Upvotes: -1

anubhava
anubhava

Reputation: 786001

There is no need to use nested echo. You can avoid it:

mysqldump my_db > ~/backups/$(date +'%Y-%m-%d_%H-%M-%S').sql

Upvotes: 5

Related Questions