0

I have the following environment variable timestamp. I am calling the environment variable by the following however the output is just date. why is it not giving the actual timestamp?

NOT assignment of the variable TIMESTAMP, but is a description of the the value that it has.

environment variable 


TIMESTAMP=date +%Y-%m-%dT%H:%M:%S.%7NZ -d '-7 day'

input  
TIMESTAMP=($TIMESTAMP)
echo "debug environment variables: $TIMESTAMP:"
    
output:
debug environment variables: date
2
  • 2
    In your code, TIMESTAMP is a shell variable, not an environment variable. Also, the code you posted does not make sense. Please show one complete script (not just snippets), and then describe what effect you see, and what you hope to see. Commented Mar 30, 2021 at 14:06
  • @user1934428 I believe the first line of the posted question is not actually an assignment of the variable TIMESTAMP, but is a description of the value that it has. (If it were an attempt at an assignment, it would fail and not set the variable at all, as it would actually just be an attempt to execute the command +%Y-%m-%dT%H:%M:%S.%7NZ with TIMESTAMP set in its environment.) The question should be edited for clarity. Commented Mar 30, 2021 at 14:35

2 Answers 2

3

Consider:

T="a b c"
T=($T)

In the above, the assignment T=($T) is equivalent to T=(a b c) which makes T an array. For the array T, $T is equivalent to ${T[0]}, so your $TIMESTAMP is just showing the first element of the array. You probably meant to write TIMESTAMP=$($TIMESTAMP), but ... that's a really bad idea. Don't do that. If you execute arbitrary code from an environment variable, you're just asking for trouble.

0

It seems that what you're attempting to create a variable using a linux command as the value and without specifing variable=$(command), therefore, your system is taking the spaces as if you're giving different commands.

Attempt to give the variable a value using TIMESTAMP=$(date +%Y-%m-%dT%H:%M:%S.%7NZ --date "-7 day") instead.