Variables in the shell and eval

Source: Internet
Author: User

Original link: http://www.361way.com/shell-eval-variable/4957.html

Nesting of variables is often used in the shell. For example, the value of a single or multiple variables as a variable name, and then the variable name to get its internal variables, this script is difficult to write, fortunately there is an eval command to facilitate our handling of this situation. Easy to understand, let's start with an example:

  1. #!/bin/bash
  2. A=' my '
  3. b=' site '
  4. My_site=' My site www.361way.com '
  5. Echo A_b is "$a"_"$b"
  6. echo $("$a"_"$b")
  7. Web="$a"_"$b"
  8. echo Web is $web
  9. Eval echo ' $ ' "$a"_"$b"
  10. Eval echo ' $ '{"$a"_"$b"}

Like the example above, guess what it's running for? The results of the implementation are as follows:

    1. # sh a.sh
    2. A_b is My_site
    3. A. SH: line 6: my_site: command not found
    4. Web is My_site
    5. My Site www. 361way. COM
    6. My Site www. 361way. COM

From the above can be seen $ ("$a" _ "$b") This writing will be error, you can correctly take the results we want can be written in two ways: eval echo ' $ ' $a "_" $b "or eval echo ' $ ' {" $a "_" $b "}.

Eval is so magical, what the hell is it?

The eval command will first scan the command line for all permutations before executing the command. This command applies to variables that scan for a variable that does not function at one time, and the command scans the variables two times. These variables, which need to be scanned two times, are sometimes referred to as complex variables. The eval command can be used to echo a simple variable, or to echo a complex variable.

Another example, in this case, the file has two columns, the first column variable name, the second column variable value, after reading the file, the value of the second row is assigned to the first column, through the Echo ${column name}, you can get the value of the subsequent value. As follows:

  1. Variable name and variable Value mapping table:
  2. # more Name_value.txt
  3. Site www. 361way. COM
  4. mail [email protected]. COM
  5. User admin
  6. Script content:
  7. # Cat B.sh
  8. #!/bin/bash
  9. While read NAME VALUE
  10. Do
  11. eval "${name}=${value}"
  12. Done < name_value. TXT
  13. echo "$site $mail $user"

Execute the script with the following results:

    1. # sh b.sh
    2. www. 361way. com [email protected]. COM admin

Variables in the shell and Eval (go)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.