Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell cmd "date" without new line in the end

Tags:

shell

newline

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?

like image 373
novice3 Avatar asked Jan 22 '16 09:01

novice3


People also ask

How do you append without a new line?

Assuming that the file does not already end in a newline and you simply want to append some more text without adding one, you can use the -n argument, e.g. However, some UNIX systems do not provide this option; if that is the case you can use printf , e.g. Do not print the trailing newline character.

How do you echo end of line?

There are a couple of different ways we can print a newline character. The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way.

What %D format in date command does?

9: List of Format specifiers used with date command: %D: Display date as mm/dd/yy. %d: Display the day of the month (01 to 31). %a: Displays the abbreviated name for weekdays (Sun to Sat).


1 Answers

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]"`" 
like image 132
gj13 Avatar answered Sep 28 '22 00:09

gj13