Home > OS >  If statements with escape characters in Alpine Linux (ash busybox)
If statements with escape characters in Alpine Linux (ash busybox)

Time:03-08

I have simple script to prepare file and it is working as intended. All code is executed at docker image alpine:latest (so ash busybox)

echo "credentials \"url.com\" {token = \"${VALUE}\"}" > ~/.terraformrc

Later I want to prepare test and I am using if [[ ]] syntax for that.

if [[ $(cat ~/.terraformrc) != "credentials \"url.com\" {token =\"VALUE\" }" ]]; then exit 1; fi

On Bash test is working but on ash/busybox/alpine I get

sh: "url.com": unknown operand

Also on ash/busybox/alpine when I do echo all is escaping correctly

echo "credentials \"url.com\" {token = \"VALUE\" }"
credentials "url.com" {token = "VALUE" }

How should I escape " inside [ ] or [[ ]] ? Thx for help

CodePudding user response:

[[ ]] is for bash, for ash you should use POSIX [ ] instead:

#!/bin/sh

value=VALUE001

echo "credentials \"url.com\" {token = \"$value\"}" > ~/.terraformrc

if [ "$(cat ~/.terraformrc)" != "credentials \"url.com\" {token =\"VALUE001\" }" ]
then
    exit 1
fi

BTW, you were doing the quote escaping correctly

  • Related