0

I am confused by how to use variables in Bash. Please see the following example. I am not able to figure out why Bash isn't able to recognize the variable within (). Can anybody please help me understand what is going on.

$echo $SHELL
 /bin/bash
$export TestC=/Users
$echo $TestC
 /Users
$export TestD=$TestC/ABCD
$echo $TestD
/Users/ABCD
$export TestD=$(TestC)/ABCD
-bash: TestC: command not found

Thanks for your help

3
  • Because that's not at all how you specify a variable name. Commented May 14, 2017 at 14:06
  • Thanks for your reply. Can you please help me understand what I am doing wrong. Commented May 14, 2017 at 14:14
  • Why did you use parentheses? Commented May 14, 2017 at 15:37

1 Answer 1

4

When referencing a bash variable you either use $ then the name, as in $TestC or you can put braces around the name like ${TestC}.

$(...) is a subshell syntax called command substitution that will execute the command inside the parens then "return" the stdout of that command.

Read all about parameter/variable expansion here, which also shows a lot of the extra things you can do with the parameter expansion when using braces.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.