Bash variable assignment before command

前端 未结 2 640
忘掉有多难
忘掉有多难 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?
    0 讨论(0)
  • 2021-01-15 06:19

    you are missing && before mkdir below is complete statement you desire:

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

    Also if its not necessary to declare dname you can do: mkdir $(date +%d_%m_%y) && cd ${dname} both will give same answer

    0 讨论(0)
提交回复
热议问题