LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   printf (is "+" in the argument is an add or concatenation) (https://www.linuxquestions.org/questions/linux-newbie-8/printf-is-in-the-argument-is-an-add-or-concatenation-4175685289/)

akira1412 11-15-2020 01:52 PM

printf (is "+" in the argument is an add or concatenation)
 
Hi there, I'm still a newbie in using terminal. Hope somebody can help......I'm quite confused does "+" mean concatenate the date or add days into date?

printf '\nIn 2007, %s \n\n' "$(date -d" 2007-09-01 17:30:24.000000000 UTC +
$(( 36 /60 /60 /24 / 2 )) days" '+%d %B')"

scasey 11-15-2020 02:20 PM

In the example posted, the 1st + sign is part of the argument to the date command...has naught to do with printf...see man date
I’d have to check man printf about the second + sign to be sure, but I’d guess it’s concatenation.

A good way to figure out things like this is to run each command separately to see what each part does.

ondoho 11-16-2020 01:22 AM

This is not SOLVED. Would OP like to clarify?

akira1412 11-16-2020 02:27 AM

Quote:

Originally Posted by ondoho (Post 6185757)
This is not SOLVED. Would OP like to clarify?

Urmm...does it mean its not the answer to my question??

pan64 11-16-2020 03:27 AM

Quote:

Originally Posted by akira1412 (Post 6185780)
Urmm...does it mean its not the answer to my question??

it is your question/thread. You can decide if it was answered/solved. What do you think?

GazL 11-16-2020 04:25 AM

The first '+' means add days to the date specified. The second '+' introduces the output format specifier of the date command. I don't know why they chose to write date to work that way instead of just making the output format a switch argument like the others, but it is what it is. Both are arguments to date and have nothing to do with printf.

Here's a broken down example that might help you understand what's going on.
Code:

basedate="2007-09-01 17:30:24.000000000 UTC"
days=200

display_date=$(date -d "$basedate + $days days" '+%Y %d %B')

printf 'In %s, %s %s\n' $display_date

Your days calculation made no sense, so I replaced it with '200', and I removed the hardcoded year in your printf format string and replaced it with an additional output field from date, for reasons that should be obvious.


P.S. Odd choice of basedate, but whatever rocks your boat. ;)

akira1412 11-16-2020 07:14 AM

Quote:

Originally Posted by GazL (Post 6185806)
The first '+' means add days to the date specified. The second '+' introduces the output format specifier of the date command. I don't know why they chose to write date to work that way instead of just making the output format a switch argument like the others, but it is what it is. Both are arguments to date and have nothing to do with printf.

Here's a broken down example that might help you understand what's going on.
Code:

basedate="2007-09-01 17:30:24.000000000 UTC"
days=200

display_date=$(date -d "$basedate + $days days" '+%Y %d %B')

printf 'In %s, %s %s\n' $display_date

Your days calculation made no sense, so I replaced it with '200', and I removed the hardcoded year in your printf format string and replaced it with an additional output field from date, for reasons that should be obvious.


P.S. Odd choice of basedate, but whatever rocks your boat. ;)

Thank you so much..... now I understand


All times are GMT -5. The time now is 12:38 AM.