Mihai Albert
Mihai Albert

Reputation: 1503

Why are spaces / newlines missing in interactive kubectl bash session

I've connected to a container inside my K8s cluster using:

kubectl -n <namespace> exec -ti <pod> -- bash

Next I'm looking at the content of files within the /proc virtual folder. So for looking at the environment variables defined for process with id 1, I do cat /proc/1/environ.

The problem is that the output is:

var1=valuevar2=valuevar3=value

instead of:

var1=value
var2=value
var3=value

The same thing happens for cat /proc/1/cmdline, as the output is

commandarg1arg2

instead of

command arg1 arg2

What's causing the newline and spaces to be omitted from the output, and how can it be corrected?

Upvotes: 0

Views: 1004

Answers (1)

user1934428
user1934428

Reputation: 22291

The pieces of the environment can not be separated by Newlines, because a Newline character could be part of the environment string itself. Imagine an environment variable with name X, where its value is AB\nY=Z. You could not distinguish it from the case that you have two variables, X and Y.

The heart of the environment managements are the system calls getenv and setenv, which are for instance present in the C language. The value of the environment variable is a C-string, i.e. a sequence of bytes terminated by a NUL-byte. In fact, NUL is the only value which is not permitted inside the value of an environment variable.

Hence what your cat shows you is the environment definitions, separated by a NUL-byte.

Upvotes: 2

Related Questions