command "$(</token/ecr-token)" is not working in sh shell but working in bash , how we can make this command work in sh shell

10/1/2021
kubectl create secret docker-registry $SECRET_NAME \
                 --dry-run=client \
                 --docker-server="$ECR_REGISTRY" \
                 --docker-username=AWS \
                 --docker-password="$(</token/ecr-token)" \
                 -o yaml

file token/ecr-token is in place but still not able to populate in --docker-password

-- kapil dev
amazon-eks
docker
kubernetes
linux
shell

1 Answer

10/1/2021

The $(< /path/to/a/file) syntax is a special form of command substitution that is supported by several UNIX shells, including ksh (Oracle online documentation), bash, and zsh. It attempts to open the listed file and then reads the contents of that file in place of the $(< ...). It is distinct from command substitution in that it does not execute the given file or its contents.

See more details at this UNIX & Linux Q/A: Understanding Bash's Read-a-File Command Substitution.

Note that your command specifies an absolute file path of /token/ecr-token but then later describe a relative path of token/ecr-token. If you're using a relative path in the command-line, that file will need to exist relative to your current working directory. Of course, the file must be readable by the current user for the contents to be read.

If your shell does not support this feature, a workaround is to use $(cat /path/to/a/file), for instance:

kubectl create secret docker-registry "$SECRET_NAME" \
                 --dry-run=client \
                 --docker-server="$ECR_REGISTRY" \
                 --docker-username=AWS \
                 --docker-password="$(cat /token/ecr-token)" \
                 -o yaml
-- Jeff Schaller
Source: StackOverflow