Bash variable assignment before command

前端 未结 2 672
忘掉有多难
忘掉有多难 2021-01-15 05:49

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

2条回答
  •  情深已故
    2021-01-15 06:08

    Shell is substituting your variable before running the command inside $(). You can use && to make it work for you:

    dname=$(date +%d_%m_%y) && mkdir ${dname} && cd ${dname}
    

    or, of course:

    dname=$(date +%d_%m_%y); mkdir ${dname} && cd ${dname}
    

    However, dname would be available to mkdir if it were to grab the environment variable inside.

    Let's say we have a script test.sh that has a single statement echo $dname inside. Then:

    dname=$(date +%d_%m_%y) ./test.sh
    

    would yield:

    07_03_17
    

    This is consistent with how your aws command line worked.


    Similar posts:

    • How do I set an environment variable on the command line and have it appear in commands?

提交回复
热议问题