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?
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.
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.
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).
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]"`"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With