0

This question already has an answer here:

I am creating a logfile from kermit named the current date (which is in the format of DD MMM YYYY - which can not be changed AFAIK), and using the file name as today's date.

I then have a Bash script to read the file and I want to tail -n1 the file.

if I do

    filename=$(date +%d\ %b\ %Y) && echo $filename

I get

    23 Feb 2015

which is what I want and expect. However if I then do

    filename=$(date +%d\ %b\ %Y) && tail -n1 $filename

I get

    tail: cannot open `23' for reading: No such file or directory
    tail: cannot open `Feb' for reading: No such file or directory
    tail: cannot open `2015' for reading: No such file or directory

I am not sure what is causing this, probably down to the the way I have string formatted I guess since I get the same if I use tail or cat?

marked as duplicate by Gilles bash Feb 23 '15 at 12:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • It is better to ask a direct question like "What causes tail to give these errors and how do I solve this?" than to ask for me specifically to offer assistance (I'm not always online, nor do I know everything). – anyone Feb 24 '15 at 15:45
1

Double-quote your variables. Double-quote your variables. Double-quote your variables.

filename=$(date +'%d %b %Y') && tail -n1 "$filename"
  • Many thanks! - it worked perfectly !! - and thanks for the explanation! - will try to remember in future! – Chummers Feb 23 '15 at 12:00
  • 2
    ITYM DOUBLE-QUOTE YOUR VARIABLES! – Stéphane Chazelas Feb 23 '15 at 12:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.