Skip to main content
Update title to reflect the discerned problem
Source Link
Magnus
  • 1.2k
  • 1
  • 9
  • 14

How to use evalpipe `terraform state show` outputs to set variables from a string in linuxbash variable?

I have a bash script where I'm trying to isolate a string of the form z="y=x" so that I can use eval $z in order to create a env variable y with value 'x'. Here's my code:

xxx=$(terraform state show aws_s3_bucket.prod_bucket | grep website_endpoint | sed 's/ //g')
echo $xxx
>>>website_endpoint="my-site.s3-website-us-east-1.amazonaws.com"
eval $xxx
>>>-bash: website_endpoint=my-site.s3-website-us-east-1.amazonaws.com: command not found

What perplexes me about the failure of this code is that I do something very similar to convert the contents of a .env file to working environment variables:

cmd=$(sed -ne '/^#/d; /^export / {p;d}; /.*=/ s/^/export / p' .env)
eval $cmd     ### works!

Why does the latter work but not the prior?

Edit: As an extra clue to what's going on here, I noticedNow that when I directedI've identified the content of $xxx to a plain text fileproblem as being with terraform and not bash, I've changed the content appeared as [1m[0mwebsite_endpoint[0m[0m="magnus-misc-v2.s3-website-us-east-1.amazonaws.com". I have no idea what these weird symbols indicate. Any idea anyone?title to reflect that

How to use eval to set variables from a string in linux?

I have a bash script where I'm trying to isolate a string of the form z="y=x" so that I can use eval $z in order to create a env variable y with value 'x'. Here's my code:

xxx=$(terraform state show aws_s3_bucket.prod_bucket | grep website_endpoint | sed 's/ //g')
echo $xxx
>>>website_endpoint="my-site.s3-website-us-east-1.amazonaws.com"
eval $xxx
>>>-bash: website_endpoint=my-site.s3-website-us-east-1.amazonaws.com: command not found

What perplexes me about the failure of this code is that I do something very similar to convert the contents of a .env file to working environment variables:

cmd=$(sed -ne '/^#/d; /^export / {p;d}; /.*=/ s/^/export / p' .env)
eval $cmd     ### works!

Why does the latter work but not the prior?

Edit: As an extra clue to what's going on here, I noticed that when I directed the content of $xxx to a plain text file, the content appeared as [1m[0mwebsite_endpoint[0m[0m="magnus-misc-v2.s3-website-us-east-1.amazonaws.com". I have no idea what these weird symbols indicate. Any idea anyone?

How to pipe `terraform state show` outputs to a bash variable?

I have a bash script where I'm trying to isolate a string of the form z="y=x" so that I can use eval $z in order to create a env variable y with value 'x'. Here's my code:

xxx=$(terraform state show aws_s3_bucket.prod_bucket | grep website_endpoint | sed 's/ //g')
echo $xxx
>>>website_endpoint="my-site.s3-website-us-east-1.amazonaws.com"
eval $xxx
>>>-bash: website_endpoint=my-site.s3-website-us-east-1.amazonaws.com: command not found

What perplexes me about the failure of this code is that I do something very similar to convert the contents of a .env file to working environment variables:

cmd=$(sed -ne '/^#/d; /^export / {p;d}; /.*=/ s/^/export / p' .env)
eval $cmd     ### works!

Why does the latter work but not the prior?

Edit: Now that I've identified the problem as being with terraform and not bash, I've changed the title to reflect that

Added clue regarding funny characters in the saved string
Source Link
Magnus
  • 1.2k
  • 1
  • 9
  • 14

I have a bash script where I'm trying to isolate a string of the form z="y=x" so that I can use eval $z in order to create a env variable y with value 'x'. Here's my code:

xxx=$(terraform state show aws_s3_bucket.prod_bucket | grep website_endpoint | sed 's/ //g')
echo $xxx
>>>website_endpoint="my-site.s3-website-us-east-1.amazonaws.com"
eval $xxx
>>>-bash: website_endpoint=my-site.s3-website-us-east-1.amazonaws.com: command not found

What perplexes me about the failure of this code is that I do something very similar to convert the contents of a .env file to working environment variables:

cmd=$(sed -ne '/^#/d; /^export / {p;d}; /.*=/ s/^/export / p' .env)
eval $cmd     ### works!

Why does the latter work but not the prior?

Edit: As an extra clue to what's going on here, I noticed that when I directed the content of $xxx to a plain text file, the content appeared as [1m[0mwebsite_endpoint[0m[0m="magnus-misc-v2.s3-website-us-east-1.amazonaws.com". I have no idea what these weird symbols indicate. Any idea anyone?

I have a bash script where I'm trying to isolate a string of the form z="y=x" so that I can use eval $z in order to create a env variable y with value 'x'. Here's my code:

xxx=$(terraform state show aws_s3_bucket.prod_bucket | grep website_endpoint | sed 's/ //g')
echo $xxx
>>>website_endpoint="my-site.s3-website-us-east-1.amazonaws.com"
eval $xxx
>>>-bash: website_endpoint=my-site.s3-website-us-east-1.amazonaws.com: command not found

What perplexes me about the failure of this code is that I do something very similar to convert the contents of a .env file to working environment variables:

cmd=$(sed -ne '/^#/d; /^export / {p;d}; /.*=/ s/^/export / p' .env)
eval $cmd     ### works!

Why does the latter work but not the prior?

I have a bash script where I'm trying to isolate a string of the form z="y=x" so that I can use eval $z in order to create a env variable y with value 'x'. Here's my code:

xxx=$(terraform state show aws_s3_bucket.prod_bucket | grep website_endpoint | sed 's/ //g')
echo $xxx
>>>website_endpoint="my-site.s3-website-us-east-1.amazonaws.com"
eval $xxx
>>>-bash: website_endpoint=my-site.s3-website-us-east-1.amazonaws.com: command not found

What perplexes me about the failure of this code is that I do something very similar to convert the contents of a .env file to working environment variables:

cmd=$(sed -ne '/^#/d; /^export / {p;d}; /.*=/ s/^/export / p' .env)
eval $cmd     ### works!

Why does the latter work but not the prior?

Edit: As an extra clue to what's going on here, I noticed that when I directed the content of $xxx to a plain text file, the content appeared as [1m[0mwebsite_endpoint[0m[0m="magnus-misc-v2.s3-website-us-east-1.amazonaws.com". I have no idea what these weird symbols indicate. Any idea anyone?

Source Link
Magnus
  • 1.2k
  • 1
  • 9
  • 14

How to use eval to set variables from a string in linux?

I have a bash script where I'm trying to isolate a string of the form z="y=x" so that I can use eval $z in order to create a env variable y with value 'x'. Here's my code:

xxx=$(terraform state show aws_s3_bucket.prod_bucket | grep website_endpoint | sed 's/ //g')
echo $xxx
>>>website_endpoint="my-site.s3-website-us-east-1.amazonaws.com"
eval $xxx
>>>-bash: website_endpoint=my-site.s3-website-us-east-1.amazonaws.com: command not found

What perplexes me about the failure of this code is that I do something very similar to convert the contents of a .env file to working environment variables:

cmd=$(sed -ne '/^#/d; /^export / {p;d}; /.*=/ s/^/export / p' .env)
eval $cmd     ### works!

Why does the latter work but not the prior?