Edccccch
Edccccch

Reputation: 29

output a file with a variable name in shell

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

Upvotes: 2

Views: 13580

Answers (2)

Krzysztof Krasoń
Krzysztof Krasoń

Reputation: 27496

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

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

Upvotes: 1

heemayl
heemayl

Reputation: 42107

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"

Upvotes: 4

Related Questions