A couple of days ago I came across a command
AWS_ACCESS_KEY="foo" AWS_SECRET_KEY="bar" aws list iam
I see that setting variables before a command adds those variables in the command's environment:
#make sure there is no environment variable "foo"
$ echo $foo
#mimic-ing above command
$ foo=bar printenv | grep foo
foo=bar
#or trying from python environment
$foo=bar python -c "import os; print(os.getenv('foo', None))"
bar
#foo is destroyed now
$ echo $foo
#<<NOTHING
I was trying to use this trick to dynamically create a new directory based on today's date:
$ dname=$(date +%d_%m_%y) mkdir ${dname} && cd ${dname}
but I got following error:
mkdir: missing operand
Try 'mkdir --help' for more information.
i.e. dname=$(date +%d_%m_%y) echo $dname
returns empty!
What am I doing wrong? How can I dynamically create and use are variable on the same line in bash?