How to store a command in a variable in a shell script?

前端 未结 8 993
长情又很酷
长情又很酷 2020-11-22 07:35

I would like to store a command to use at a later period in a variable (not the output of the command, but the command itself)

I have a simple script as follows:

相关标签:
8条回答
  • 2020-11-22 08:04
    var=$(echo "asdf")
    echo $var
    # => asdf
    

    Using this method, the command is immediately evaluated and it's return value is stored.

    stored_date=$(date)
    echo $stored_date
    # => Thu Jan 15 10:57:16 EST 2015
    # (wait a few seconds)
    echo $stored_date
    # => Thu Jan 15 10:57:16 EST 2015
    

    Same with backtick

    stored_date=`date`
    echo $stored_date
    # => Thu Jan 15 11:02:19 EST 2015
    # (wait a few seconds)
    echo $stored_date
    # => Thu Jan 15 11:02:19 EST 2015
    

    Using eval in the $(...) will not make it evaluated later

    stored_date=$(eval "date")
    echo $stored_date
    # => Thu Jan 15 11:05:30 EST 2015
    # (wait a few seconds)
    echo $stored_date
    # => Thu Jan 15 11:05:30 EST 2015
    

    Using eval, it is evaluated when eval is used

    stored_date="date" # < storing the command itself
    echo $(eval "$stored_date")
    # => Thu Jan 15 11:07:05 EST 2015
    # (wait a few seconds)
    echo $(eval "$stored_date")
    # => Thu Jan 15 11:07:16 EST 2015
    #                     ^^ Time changed
    

    In the above example, if you need to run a command with arguments, put them in the string you are storing

    stored_date="date -u"
    # ...
    

    For bash scripts this is rarely relevant, but one last note. Be careful with eval. Eval only strings you control, never strings coming from an untrusted user or built from untrusted user input.

    • Thanks to @CharlesDuffy for reminding me to quote the command!
    0 讨论(0)
  • 2020-11-22 08:06

    Use eval:

    x="ls | wc"
    eval "$x"
    y=$(eval "$x")
    echo "$y"
    
    0 讨论(0)
  • 2020-11-22 08:16

    I tried various different methods:

    printexec() {
      printf -- "\033[1;37m$\033[0m"
      printf -- " %q" "$@"
      printf -- "\n"
      eval -- "$@"
      eval -- "$*"
      "$@"
      "$*"
    }
    

    Output:

    $ printexec echo  -e "foo\n" bar
    $ echo -e foo\\n bar
    foon bar
    foon bar
    foo
     bar
    bash: echo -e foo\n bar: command not found
    

    As you can see, only the third one, "$@" gave the correct result.

    0 讨论(0)
  • 2020-11-22 08:16

    Be Careful registering an order with the: X=$(Command)

    This one is still executed Even before being called. To check and confirm this, you cand do:

    echo test;
    X=$(for ((c=0; c<=5; c++)); do
    sleep 2;
    done);
    echo note the 5 seconds elapsed
    
    0 讨论(0)
  • 2020-11-22 08:22

    Its is not necessary to store commands in variables even as you need to use it later. just execute it as per normal. If you store in variable, you would need some kind of eval statement or invoke some unnecessary shell process to "execute your variable".

    0 讨论(0)
  • 2020-11-22 08:25
    #!/bin/bash
    #Note: this script works only when u use Bash. So, don't remove the first line.
    
    TUNECOUNT=$(ifconfig |grep -c -o tune0) #Some command with "Grep".
    echo $TUNECOUNT                         #This will return 0 
                                        #if you don't have tune0 interface.
                                        #Or count of installed tune0 interfaces.
    
    0 讨论(0)
提交回复
热议问题