45

I want to output a line of date using date command, but it's end with a "\n" which I don't need it, currently, I use:

echo -n `date +"[%m-%d %H:%M:%S]"`

or

date +"[%m-%d %H:%M:%S]"|tr -d "\n"

Are there any built-in parameter for "date" to do that?

novice3
  • 556
  • 1
  • 5
  • 9
  • 3
    Hey novice3; can you select an answer? – SanThee Jun 15 '17 at 11:09
  • @Bamboomy Or the new user should specify in a comment somewhere, how all the answers are falling short, in case he's looking for something that none of us are considerating. – L. D. James May 01 '18 at 11:30

5 Answers5

32

No there isn't. You need another command like echo -n, printf or tr. You could put a script somewhere in your PATH (eg. /usr/bin/) and make it executable with chmod +x /usr/bin/mydate

script:

#!/bin/sh
echo -n `date +"[%m-%d %H:%M:%S]"`

or use an alias.

alias mydate="echo -n `date +"[%m-%d %H:%M:%S]"`"
gj13
  • 1,314
  • 9
  • 23
28

Bash printf does not require date to work:

printf '%(%m-%d %H:%M:%S)T'

will output the current time without a newline.

radio_tech
  • 671
  • 7
  • 5
17

You can use printf. It doesn't add new line symbol:

$ printf `date "+%d.%m.%Y"`
22.01.2016$
xxfelixxx
  • 6,512
  • 3
  • 31
  • 38
Dmitry Tabakov
  • 374
  • 1
  • 7
0

You are going to want to replace the newline with some form of space so output doesn't run together.

Full example which can be used to monitor peak size of a workspace:

nohup watch -t -n 60 "(date '+%F/%H:%M:%S' | tr '\n' '\t'; du -sh /folder/to/monitor/) >> workspace.log" &

Don't forget to kill this as the file would grow indefinitely.

Keiran Raine
  • 93
  • 1
  • 8
0

I guess you want to add something after date in each line. Instead of using date without EOL you can do it other way around. Inside date command you can add ouput of any other command by using $() or backticks ``. Final output is the same, date and then something in each line.

For example:

date "+%F %T $(echo 123)"

will print date and then 123 in same line.

blur
  • 189
  • 1
  • 6