简体   繁体   中英

Want to store a string into a file, with the filename being the output of a command in shell

I want to store a string in a file and the name of the file is the concatenation of the output of the whoami command and the output of filename="date +'%D-%H-%M'". currently I am doing something like

user=$whoami
echo "Sample String" > $user

But it is not working. Any suggestions on what I am doing wrong? Also, as I mentioned above I want to concat the output of the $user and $filename as the final name of the file. Thanks in advance.

You can do the following:

name="$(date +'%d%m%Y-%H-%M')_$(whoami)"
echo "Sample string" >> $name

Then we have:

$ ls -ltr
-rw-rw-r--  1 me me    6 Jun 24 17:00 24062013-17-00_me

Your

user=$whoami

was not working because it should be user=$(whoami) . In general, to store a command output you have to do variable=$(command) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM