Solution1: "just use plain echo with double-quotes around the variable" -Gordon Davidson
$: MYVARVALUE=some-new-domain
$: template=$(cat "file.yaml" | sed "s/domain/$MYVARVALUE/g")
$: echo "$template"
Which gives the following stdout
:
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: dashboarding-route
spec:
host: dashboarding-route.some-new-domain
port:
targetPort: 100
tls:
termination: passthrough
to:
kind: Service
name: dashboarding
Solution2: use printf
instead of echo
You could use printf
instead of echo
to preserve the yaml
formatting, like so:
$: MYVARVALUE=some-new-domain
$: template=$(cat "file.yaml" | sed "s/domain/$MYVARVALUE/g")
$: printf '%s\n' "$template"
Which gives the following stdout
:
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: dashboarding-route
spec:
host: dashboarding-route.some-new-domain
port:
targetPort: 100
tls:
termination: passthrough
to:
kind: Service
name: dashboarding
Solution3: encode/decode stdout
using base64
Another option to preserve formatting is to encode the data when storing/transmitting it, and then decode it when accessing/receiving it:
$: MYVARVALUE=some-new-domain
$: template=$(cat "file.yaml" | sed "s/domain/$MYVARVALUE/g" | base64 -w 0)
$: echo "$template" | base64 -d
Which also gives the following stdout
:
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: dashboarding-route
spec:
host: dashboarding-route.some-new-domain
port:
targetPort: 100
tls:
termination: passthrough
to:
kind: Service
name: dashboarding
This last solution is particularly useful for moving formatting-sensitive data such as ssh-keys or hash-values between machines; just be aware the this is not a substitute for encryption!