output a file with a variable name in shell

2019-09-10 04:18发布

So I am trying to output a file with the name of like: lastlogin-"yyyymmdd" where it contains the current date. I figured out the date should be : date +"%Y%m%d" and I tried to do a variable

now = date +"lastlogin-%Y%m%d.txt" filename = ${now} xxxxx > ${filename} but nothing seems to work

Please help

2条回答
Summer. ? 凉城
2楼-- · 2019-09-10 05:01

Use command substitution:

lastlogin-"$(date '+%Y%m%d')".txt

To save in a variable:

filename="lastlogin-"$(date '+%Y%m%d')".txt"

and then do:

echo 'foobar' >"$filename"
查看更多
不美不萌又怎样
3楼-- · 2019-09-10 05:07

You should use $() for command execution and storage of result:

now=$(date +"lastlogin-%Y%m%d.txt")
查看更多
登录 后发表回答