0

I have a short bash script to take in an int from a user to identify to select a certain eks cluster.

When I use eval $(aws eks --region us-east-1 update-kubeconfig --name eventplatform$input)

I get the error: Updated: command not found

This is resolved if I simply change the command to just aws eks --region us-east-1 update-kubeconfig --name eventplatform$input

Why is the eval command not working for this?

#!/usr/bin/env bash -e

read -p "Which cluster are you trying to access? (int >= 0): " input

while [[ -n ${input//[0-9]/} ]]
do
    read -p "Which cluster are you trying to access? (int >= 0): " input
done

eval $(aws eks --region us-east-1 update-kubeconfig --name eventplatform$input)
5
  • 2
    set -x is your friend to enable trace logging so you can see what's going on. That said, you almost certainly shouldn't be using eval here. What's the point of it? Commented Jul 19, 2022 at 19:20
  • 1
    (btw, set -e is generally a bad idea; it makes your script far harder for readers/reviewers to understand its behavior, not to mention authors. See the exercises section of BashFAQ #105) Commented Jul 19, 2022 at 19:21
  • 1
    The command you are using modifies your ~/.kube/config file. It does not output text that can be executed by a shell. Commented Jul 19, 2022 at 19:21
  • 1
    ...also, if you are going to use eval, there is basically no circumstance where eval $(something) is more correct than eval "$(something)" -- the former breaks the output of something into words and expands each word as a glob expression before combining them back into a single string and trying to parse that string as code. But collect the xtrace log so we know what's happening during execution before trying to go down that road. Commented Jul 19, 2022 at 19:22
  • 1
    (also, it's arguably a bug that AWS is emitting their "Updated" message to stdout instead of stderr, which is where POSIX states that diagnostic data -- a category which includes status updates meant for human readers -- belongs). Commented Jul 19, 2022 at 19:25

2 Answers 2

2

The eval command isn't supposed to work for this (whatever you expect "work" to mean in this context).

eval runs data as code. Only programs that write well-formed shell commands or scripts to their stdout as data should have that data evaled.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. Eval is rarely useful. Eval is for advanced users, and most advanced users usually know better than to use it.
1

Simple. eval argument should be a string or something that exists to run, not output of other comments

#!/bin/bash

eval "ls" # okay 
# file1.txt
# file2.txt

eval $(ls); # error
# bash: file1.txt: command not found

See help eval

eval: eval [arg ...] Execute arguments as a shell command.

Combine ARGs into a single string, use the result as input to the shell,
and execute the resulting commands.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.