Bash - Date command and space
The correct approach is to define your own function inside your Bash script.
function my_date {
date "+%y-%m-%d %H:%M:%S"
}
Now you can use my_date
as if it were an external program.
For example:
echo "It is now $(my_date)."
Or simply:
my_date
Why isn't your approach working?
The first problem is that your assignment is broken.
DATE_COMMAND="date "+%y-%m-%d %H:%M:%S""
This is parsed as an assignment of the string date +%y-%m-%d
to the variable DATE_COMMAND
. After the blank, the shell starts interpreting the remaining symbols in ways you did not intend.
This could be partially fixed by changing the quotation.
DATE_COMMAND="date '+%y-%m-%d %H:%M:%S'"
However, this doesn't really solve the problem because if we now use
echo $($DATE_COMMAND)
It will not expand the argument correctly. The date
program will see the arguments '+%y-%m-%d
and %H:%M:%S'
(with quotes) instead of a single string. This could be solved by using eval
as in
DATE_COMMAND="date '+%y-%m-%d %H:%M:%S'"
echo $(eval $DATE_COMMAND)
where the variable DATE_COMMAND
is first expanded to the string date '+%y-%m-%d %H:%M:%S'
that is then eval
uated as if it were written like so thus invoking date
correctly.
Note that I'm only showing this to explain the issue. eval
is not a good solution here. Use a function instead.
PS It is better to avoid all-uppercase identifier strings as those are often in conflict with environment variables or even have a magic meaning to the shell.
bash is eating spaces from date format in linux
Use double-quotes like this:
$ echo $(date -d '+1 day' '+%b %e')
Aug 2
$ echo "$(date -d '+1 day' '+%b %e')"
Aug 2
Or:
$ var="$(date -d '+1 day' '+%b %e')"
$ echo $var
Aug 2
$ echo "$var"
Aug 2
Without double-quotes, the shell, among other things, applies word splitting to the output and that collapses multiple spaces to one blank.
Bash date command and extra space when using quotes
Let us run only date
command and see what output it gives us.
date
Thu Dec 6 10:37:15 EST 2018
Since "
keeps original spaces save in them so when you are running your 2nd command as follows then it keeps that space save.
echo "Started at $(date)"
Started at Thu Dec 6 10:36:46 EST 2018
So we should actually say it is NOT adding extra space instead of keeping the original space in output.
Why is echoing a quoted date bash command adding a space?
Your date
command prints an extra space before the day-of-month if the day-of-month is a single digit.
Here's your first command:
echo $(date)
Since you didn't put double-quotes around $(date)
, the shell performs word-splitting on the output of date
, and passes each word as a separate argument to echo
. In word-splitting, the shell considers two consecutive spaces to mean the same thing as a single space. So the shell “eats” the extra space emitted by date
.
Here's your second version:
echo "$(date)"
Since you put double-quotes around $(date)
, the shell does not perform word-splitting on the output of date
. It passes the entire output of date
, except for trailing newlines, as a single argument to echo
. So the shell preserves the extra space that was output by date
.
bash datetime concatenated with space
$ date +"%Y-%m-%d %H:%M:%S"
2015-10-13 14:48:17
date +"%FORMAT %FORMAT" # just an example with space
Anything other than %FORMAT remains the same.
space issue between Month and date in linux
date
is outputting the string you want; I suspect you aren't properly quoting the variable value when you expand it.
$ d=$(date +'%b %_d %H')
$ echo $d
Jun 6 20
$ echo "$d"
Jun 6 20
split date and time collide with space between them in bash
Just use cut
root@a036fb1c94fa:~# DATE=$(echo "2020-02-2514:24:25" | cut -b-10)
root@a036fb1c94fa:~# TIME=$(echo "2020-02-2514:24:25" | cut -b11-)
root@a036fb1c94fa:~# TIMESTAMP=$(date -d "$DATE $TIME" +"%s")
root@a036fb1c94fa:~# echo $TIME
14:24:25
root@a036fb1c94fa:~# echo $DATE
2020-02-25
root@a036fb1c94fa:~# echo $TIMESTAMP
1582637065
Explanation:
echo "2020-02-2514:24:25" | cut -b-10
echo the string and cut it before the 10th byte
echo "2020-02-2514:24:25" | cut -b11-
echo the string and cut it from the 11th byte until the end
echo date -d "$DATE $TIME" +"%s"
give the right format to unix date command, with +"%s" to get its timestamp
Is there a date/time format that does not have spaces?
I found a simple work around. Simply use underscores for spaces and do a tr
in the CGI script before converting to a date. It looks something like this:
stamp="$(echo $stamp|tr _ ' '|xargs -0 date -d)"
Then use a date that looks something like this:
26_Oct_2010_11:57:56_CDT
which converts to:
date -d "26 Oct 2010 11:57:56 CDT"
Date - Month in bash without leading 0 or space?
You have already removed the padding for the day, why not do the same for the month?
$ date +'%-d-%-m-%Y'
7-1-2019
Here's a list of all padding modifiers from man date
:
By default, date pads numeric fields with zeroes. The following
optional flags may follow '%':
-
(hyphen) do not pad the field
_
(underscore) pad with spaces
0
(zero) pad with zeros
^
use upper case if possible
#
use opposite case if possibleAfter any flags comes an optional field width, as a decimal number;
then an optional modifier, which is either E to use the locale's
alternate representations if available, or O to use the locale's
alternate numeric symbols if available.
date with tabs space in between in unix
If you use the bash shell:
$ date +"%y"$'\t'"%m"$'\t'"%d"
10 12 15
Note the $'\t'
shell notation which produces a literal horizontal tab character.
EDIT:
Many commands, such as printf(1)
support some kind of notation to specify any special characters like a horizontal tab:
$ printf 'x\ty\n'
x y
If the command does not support something like this, the way to deal with it is to embed a literal horizontal tab in the format string. Unfortunately the shell tends to treat the tab key specially e.g. to perform auto-completion.
In bash (and perhaps other sh-like shells) there is a convenient notation like $'\t'
to get around this issue.
Another way is to invoke the "quote mode" of the shell. In many shells (including bash and tcsh) this can be done by pressing Ctrl-V before pressing the special character. This will enable the shell's quote mode for the next character only:
$ echo 'x q'
x q
The result above can be achieved by pressing Ctrl-V after the x and before pressing the tab key.
Related Topics
Srlua Makefile Error Lua.H No Such File or Directory
How to Grep for Strings with Special Characters Like []
Posix_Fadvise(Willneed) Makes Io Slower
How to Sort Numbers in a Qtreewidget Column
Gnu Find: When Does The Default Action Apply
How Does This Perl One Liner in The Bash Works
Exit from Bash Script But Keep The Process Running
How to Recursively Unzip Nested Zip Files
In Bash, How to Not Create The Redirect Output File Once The Command Fails
Why The Process Is Getting Killed at 4Gb
How to Create a Virtual Device in Linux
Change The Default Find-Grep Command in Emacs
Exploiting a String-Based Overflow on X86-64 with Nx (Dep) and Aslr Enabled
Increase Mongodb Maximum Number of Connections