Set variable in current shell from awk

后端 未结 7 1923
花落未央
花落未央 2020-12-29 05:40

Is there a way to set a variable in my current shell from within awk?

I\'d like to do some processing on a file and print out some data; since I\'ll rea

7条回答
  •  粉色の甜心
    2020-12-29 06:09

    $ echo "$var"
    
    $ declare $( awk 'BEGIN{print "var=17"}' )
    $ echo "$var"
    17
    

    Here's why you should use declare instead of eval:

    $ eval $( awk 'BEGIN{print "echo \"removing all of your files, ha ha ha....\""}' )
    removing all of your files, ha ha ha....
    
    $ declare $( awk 'BEGIN{print "echo \"removing all of your files\""}' )
    bash: declare: `"removing': not a valid identifier
    bash: declare: `files"': not a valid identifier
    

    Note in the first case that eval executes whatever string awk prints, which could accidentally be a very bad thing!

提交回复
热议问题