Why is there a single space before the output of `uptime`?
Perhaps a trivial question, but I found myself in the situation of needing to strip the leading space off the output of uptime
and it got me wondering why it was there in the first place.
An example from a Debian Stretch EC2 instance:
admin@ip-88-88-88-88:~$ uptime
22:14:50 up 7 min, 1 user, load average: 1.61, 1.21, 0.60
admin@ip-88-88-88-88:~$
I thought maybe it was padding, but for what? A three-digit hour for the clock?
linux debian uptime
add a comment |
Perhaps a trivial question, but I found myself in the situation of needing to strip the leading space off the output of uptime
and it got me wondering why it was there in the first place.
An example from a Debian Stretch EC2 instance:
admin@ip-88-88-88-88:~$ uptime
22:14:50 up 7 min, 1 user, load average: 1.61, 1.21, 0.60
admin@ip-88-88-88-88:~$
I thought maybe it was padding, but for what? A three-digit hour for the clock?
linux debian uptime
add a comment |
Perhaps a trivial question, but I found myself in the situation of needing to strip the leading space off the output of uptime
and it got me wondering why it was there in the first place.
An example from a Debian Stretch EC2 instance:
admin@ip-88-88-88-88:~$ uptime
22:14:50 up 7 min, 1 user, load average: 1.61, 1.21, 0.60
admin@ip-88-88-88-88:~$
I thought maybe it was padding, but for what? A three-digit hour for the clock?
linux debian uptime
Perhaps a trivial question, but I found myself in the situation of needing to strip the leading space off the output of uptime
and it got me wondering why it was there in the first place.
An example from a Debian Stretch EC2 instance:
admin@ip-88-88-88-88:~$ uptime
22:14:50 up 7 min, 1 user, load average: 1.61, 1.21, 0.60
admin@ip-88-88-88-88:~$
I thought maybe it was padding, but for what? A three-digit hour for the clock?
linux debian uptime
linux debian uptime
asked 6 hours ago
smitellismitelli
15318
15318
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Because the code explicitly puts it there:
pos = sprintf(buf, " %02d:%02d:%02d ",
realtime->tm_hour, realtime->tm_min, realtime->tm_sec);
The uptime
executable calls a function called print_uptime()
. This function lives in proc/whattime.c
in the procps
sources. The function just outputs a string built by sprint_uptime()
in the same C source file which constructs the output string bit by bit.
The first bit that is added to the string is added with an initial space, as shown above.
The space has been there since at least 2002 in the procps
implementation of uptime
.
Note that uptime -p
does not output a space:
$ uptime -p
up 8 weeks, 8 hours, 41 minutes
$ uptime --version
uptime from procps-ng 3.3.12
As noted by maxxvw, the GNU coreutils version of the utility uses a similar kind of output.
The uptime
utility on BSD systems does not follow the same output format:
$ uptime
11:56PM up 23:58, 1 user, load averages: 0.04, 0.02, 0.00
add a comment |
Coreutils source code is here: https://github.com/coreutils/coreutils/blob/master/src/uptime.c
As you can see, it is just as the authors decided to print it, a space before current time, two spaces after uptime, none around users connected and two spaces before load average:
fprintftime (stdout, _(" %H:%M:%S "), tmn, 0, 0);
printf (_("up %2d:%02d, "), uphours, upmins);
printf (ngettext ("%lu user", "%lu users", select_plural (entries)),
printf (_(", load average: %.2f"), avg[0]);
@jsotola No, the spaces are explicit in the calls toprintf()
.
– Kusalananda
5 hours ago
add a comment |
It's possible the present-day GNU/Linux versions of uptime are trying to be compatible with the old BSD uptime from ~1980, which used the %3d
format to print the hours portion (always <= 24) of the time. The same function was used to print both the current time-of-day and the system uptime.
if (tim >= 60) {
printf("%3d:", tim/60);
didhrs++;
} else {
printf(" ");
}
tim %= 60;
if (tim > 0 || didhrs) {
printf(didhrs&&tim<10 ? "%02d" : "%2d", tim);
} else {
printf(" ");
}
The fact that it printed sequences of spaces instead of zeroes (rather than printing nothing) may indicate the code was taken from another program where precise column positioning was needed.
It has beenstrftime()
with no leading space for the past 25 years, of course. This would not be the only part of the procps toolset that preserves very old BSD behaviour from the 1980s thirty years later, behaviour that BSD had actually already discarded by 1993/1994. BSDps
was usinggetopt()
and its manual page documenting options with leading minuses by then, too. So this seems to be a likely possibility.
– JdeBP
3 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507831%2fwhy-is-there-a-single-space-before-the-output-of-uptime%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Because the code explicitly puts it there:
pos = sprintf(buf, " %02d:%02d:%02d ",
realtime->tm_hour, realtime->tm_min, realtime->tm_sec);
The uptime
executable calls a function called print_uptime()
. This function lives in proc/whattime.c
in the procps
sources. The function just outputs a string built by sprint_uptime()
in the same C source file which constructs the output string bit by bit.
The first bit that is added to the string is added with an initial space, as shown above.
The space has been there since at least 2002 in the procps
implementation of uptime
.
Note that uptime -p
does not output a space:
$ uptime -p
up 8 weeks, 8 hours, 41 minutes
$ uptime --version
uptime from procps-ng 3.3.12
As noted by maxxvw, the GNU coreutils version of the utility uses a similar kind of output.
The uptime
utility on BSD systems does not follow the same output format:
$ uptime
11:56PM up 23:58, 1 user, load averages: 0.04, 0.02, 0.00
add a comment |
Because the code explicitly puts it there:
pos = sprintf(buf, " %02d:%02d:%02d ",
realtime->tm_hour, realtime->tm_min, realtime->tm_sec);
The uptime
executable calls a function called print_uptime()
. This function lives in proc/whattime.c
in the procps
sources. The function just outputs a string built by sprint_uptime()
in the same C source file which constructs the output string bit by bit.
The first bit that is added to the string is added with an initial space, as shown above.
The space has been there since at least 2002 in the procps
implementation of uptime
.
Note that uptime -p
does not output a space:
$ uptime -p
up 8 weeks, 8 hours, 41 minutes
$ uptime --version
uptime from procps-ng 3.3.12
As noted by maxxvw, the GNU coreutils version of the utility uses a similar kind of output.
The uptime
utility on BSD systems does not follow the same output format:
$ uptime
11:56PM up 23:58, 1 user, load averages: 0.04, 0.02, 0.00
add a comment |
Because the code explicitly puts it there:
pos = sprintf(buf, " %02d:%02d:%02d ",
realtime->tm_hour, realtime->tm_min, realtime->tm_sec);
The uptime
executable calls a function called print_uptime()
. This function lives in proc/whattime.c
in the procps
sources. The function just outputs a string built by sprint_uptime()
in the same C source file which constructs the output string bit by bit.
The first bit that is added to the string is added with an initial space, as shown above.
The space has been there since at least 2002 in the procps
implementation of uptime
.
Note that uptime -p
does not output a space:
$ uptime -p
up 8 weeks, 8 hours, 41 minutes
$ uptime --version
uptime from procps-ng 3.3.12
As noted by maxxvw, the GNU coreutils version of the utility uses a similar kind of output.
The uptime
utility on BSD systems does not follow the same output format:
$ uptime
11:56PM up 23:58, 1 user, load averages: 0.04, 0.02, 0.00
Because the code explicitly puts it there:
pos = sprintf(buf, " %02d:%02d:%02d ",
realtime->tm_hour, realtime->tm_min, realtime->tm_sec);
The uptime
executable calls a function called print_uptime()
. This function lives in proc/whattime.c
in the procps
sources. The function just outputs a string built by sprint_uptime()
in the same C source file which constructs the output string bit by bit.
The first bit that is added to the string is added with an initial space, as shown above.
The space has been there since at least 2002 in the procps
implementation of uptime
.
Note that uptime -p
does not output a space:
$ uptime -p
up 8 weeks, 8 hours, 41 minutes
$ uptime --version
uptime from procps-ng 3.3.12
As noted by maxxvw, the GNU coreutils version of the utility uses a similar kind of output.
The uptime
utility on BSD systems does not follow the same output format:
$ uptime
11:56PM up 23:58, 1 user, load averages: 0.04, 0.02, 0.00
edited 5 hours ago
answered 6 hours ago
KusalanandaKusalananda
137k17258426
137k17258426
add a comment |
add a comment |
Coreutils source code is here: https://github.com/coreutils/coreutils/blob/master/src/uptime.c
As you can see, it is just as the authors decided to print it, a space before current time, two spaces after uptime, none around users connected and two spaces before load average:
fprintftime (stdout, _(" %H:%M:%S "), tmn, 0, 0);
printf (_("up %2d:%02d, "), uphours, upmins);
printf (ngettext ("%lu user", "%lu users", select_plural (entries)),
printf (_(", load average: %.2f"), avg[0]);
@jsotola No, the spaces are explicit in the calls toprintf()
.
– Kusalananda
5 hours ago
add a comment |
Coreutils source code is here: https://github.com/coreutils/coreutils/blob/master/src/uptime.c
As you can see, it is just as the authors decided to print it, a space before current time, two spaces after uptime, none around users connected and two spaces before load average:
fprintftime (stdout, _(" %H:%M:%S "), tmn, 0, 0);
printf (_("up %2d:%02d, "), uphours, upmins);
printf (ngettext ("%lu user", "%lu users", select_plural (entries)),
printf (_(", load average: %.2f"), avg[0]);
@jsotola No, the spaces are explicit in the calls toprintf()
.
– Kusalananda
5 hours ago
add a comment |
Coreutils source code is here: https://github.com/coreutils/coreutils/blob/master/src/uptime.c
As you can see, it is just as the authors decided to print it, a space before current time, two spaces after uptime, none around users connected and two spaces before load average:
fprintftime (stdout, _(" %H:%M:%S "), tmn, 0, 0);
printf (_("up %2d:%02d, "), uphours, upmins);
printf (ngettext ("%lu user", "%lu users", select_plural (entries)),
printf (_(", load average: %.2f"), avg[0]);
Coreutils source code is here: https://github.com/coreutils/coreutils/blob/master/src/uptime.c
As you can see, it is just as the authors decided to print it, a space before current time, two spaces after uptime, none around users connected and two spaces before load average:
fprintftime (stdout, _(" %H:%M:%S "), tmn, 0, 0);
printf (_("up %2d:%02d, "), uphours, upmins);
printf (ngettext ("%lu user", "%lu users", select_plural (entries)),
printf (_(", load average: %.2f"), avg[0]);
answered 5 hours ago
maxxvwmaxxvw
18818
18818
@jsotola No, the spaces are explicit in the calls toprintf()
.
– Kusalananda
5 hours ago
add a comment |
@jsotola No, the spaces are explicit in the calls toprintf()
.
– Kusalananda
5 hours ago
@jsotola No, the spaces are explicit in the calls to
printf()
.– Kusalananda
5 hours ago
@jsotola No, the spaces are explicit in the calls to
printf()
.– Kusalananda
5 hours ago
add a comment |
It's possible the present-day GNU/Linux versions of uptime are trying to be compatible with the old BSD uptime from ~1980, which used the %3d
format to print the hours portion (always <= 24) of the time. The same function was used to print both the current time-of-day and the system uptime.
if (tim >= 60) {
printf("%3d:", tim/60);
didhrs++;
} else {
printf(" ");
}
tim %= 60;
if (tim > 0 || didhrs) {
printf(didhrs&&tim<10 ? "%02d" : "%2d", tim);
} else {
printf(" ");
}
The fact that it printed sequences of spaces instead of zeroes (rather than printing nothing) may indicate the code was taken from another program where precise column positioning was needed.
It has beenstrftime()
with no leading space for the past 25 years, of course. This would not be the only part of the procps toolset that preserves very old BSD behaviour from the 1980s thirty years later, behaviour that BSD had actually already discarded by 1993/1994. BSDps
was usinggetopt()
and its manual page documenting options with leading minuses by then, too. So this seems to be a likely possibility.
– JdeBP
3 hours ago
add a comment |
It's possible the present-day GNU/Linux versions of uptime are trying to be compatible with the old BSD uptime from ~1980, which used the %3d
format to print the hours portion (always <= 24) of the time. The same function was used to print both the current time-of-day and the system uptime.
if (tim >= 60) {
printf("%3d:", tim/60);
didhrs++;
} else {
printf(" ");
}
tim %= 60;
if (tim > 0 || didhrs) {
printf(didhrs&&tim<10 ? "%02d" : "%2d", tim);
} else {
printf(" ");
}
The fact that it printed sequences of spaces instead of zeroes (rather than printing nothing) may indicate the code was taken from another program where precise column positioning was needed.
It has beenstrftime()
with no leading space for the past 25 years, of course. This would not be the only part of the procps toolset that preserves very old BSD behaviour from the 1980s thirty years later, behaviour that BSD had actually already discarded by 1993/1994. BSDps
was usinggetopt()
and its manual page documenting options with leading minuses by then, too. So this seems to be a likely possibility.
– JdeBP
3 hours ago
add a comment |
It's possible the present-day GNU/Linux versions of uptime are trying to be compatible with the old BSD uptime from ~1980, which used the %3d
format to print the hours portion (always <= 24) of the time. The same function was used to print both the current time-of-day and the system uptime.
if (tim >= 60) {
printf("%3d:", tim/60);
didhrs++;
} else {
printf(" ");
}
tim %= 60;
if (tim > 0 || didhrs) {
printf(didhrs&&tim<10 ? "%02d" : "%2d", tim);
} else {
printf(" ");
}
The fact that it printed sequences of spaces instead of zeroes (rather than printing nothing) may indicate the code was taken from another program where precise column positioning was needed.
It's possible the present-day GNU/Linux versions of uptime are trying to be compatible with the old BSD uptime from ~1980, which used the %3d
format to print the hours portion (always <= 24) of the time. The same function was used to print both the current time-of-day and the system uptime.
if (tim >= 60) {
printf("%3d:", tim/60);
didhrs++;
} else {
printf(" ");
}
tim %= 60;
if (tim > 0 || didhrs) {
printf(didhrs&&tim<10 ? "%02d" : "%2d", tim);
} else {
printf(" ");
}
The fact that it printed sequences of spaces instead of zeroes (rather than printing nothing) may indicate the code was taken from another program where precise column positioning was needed.
edited 1 hour ago
answered 4 hours ago
Mark PlotnickMark Plotnick
18.6k24067
18.6k24067
It has beenstrftime()
with no leading space for the past 25 years, of course. This would not be the only part of the procps toolset that preserves very old BSD behaviour from the 1980s thirty years later, behaviour that BSD had actually already discarded by 1993/1994. BSDps
was usinggetopt()
and its manual page documenting options with leading minuses by then, too. So this seems to be a likely possibility.
– JdeBP
3 hours ago
add a comment |
It has beenstrftime()
with no leading space for the past 25 years, of course. This would not be the only part of the procps toolset that preserves very old BSD behaviour from the 1980s thirty years later, behaviour that BSD had actually already discarded by 1993/1994. BSDps
was usinggetopt()
and its manual page documenting options with leading minuses by then, too. So this seems to be a likely possibility.
– JdeBP
3 hours ago
It has been
strftime()
with no leading space for the past 25 years, of course. This would not be the only part of the procps toolset that preserves very old BSD behaviour from the 1980s thirty years later, behaviour that BSD had actually already discarded by 1993/1994. BSD ps
was using getopt()
and its manual page documenting options with leading minuses by then, too. So this seems to be a likely possibility.– JdeBP
3 hours ago
It has been
strftime()
with no leading space for the past 25 years, of course. This would not be the only part of the procps toolset that preserves very old BSD behaviour from the 1980s thirty years later, behaviour that BSD had actually already discarded by 1993/1994. BSD ps
was using getopt()
and its manual page documenting options with leading minuses by then, too. So this seems to be a likely possibility.– JdeBP
3 hours ago
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507831%2fwhy-is-there-a-single-space-before-the-output-of-uptime%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown