Robert D Weir
Robert D Weir

Reputation: 139

Cannot assign Variable in Bash

I am trying to assign a variable 'email-addr' to the value (only one) in a file. The code I have attempted to use throws an error. I only want to assign the contents of a file, one email address to a variable string.

 email-addr=`$(cat ../address)`

The contents of ../address is only one line

[email protected] 

It says:

[email protected]
cat: invalid option -- 'a'
Try `cat --help' for more information.

What am I doing wrong?

Upvotes: 1

Views: 1288

Answers (2)

gniourf_gniourf
gniourf_gniourf

Reputation: 46823

If you only have one email address in the first line of file ../address and you want to assign to a variable (as already mention, a variable name can't have a hyphen), please consider the following standard way of doing:

read -r my_email < ../address

Your my_email=$(cat ../address) is not how we would perform this task in bash.

Upvotes: 0

Barmar
Barmar

Reputation: 780843

Try:

my_email=$(cat ../address)

Backticks and $(...) both execute the command inside and substitute the value. So you're executing the output of cat ../address as another command.

And - isn't allowed in variable names, I've used _ instead.

I'm not sure why you're getting the error about an invalid option from cat, I don't see anything in what you've posted that would cause that. I suspect it's from something else in your script.

Upvotes: 1

Related Questions