Indirect references to variables in the shell

Source: Internet
Author: User

In the process of writing a shell script, it is believed that you will encounter this scenario:

#!/bin/sh#log_1= "xxxxxx" log_2= "yyyyyy" log_3= "zzzzzz" for num in {1..3}do log=log_$num #此时你想把log_1, log_2,log_3, these three variables            The value of the variable is referenced in the For loop sequentially, but if you echo #出的变量 directly, you will only output the variable name of the variable you want to refer to, not the variable value of the variable, you need to use the shell #变量的间接引用了, you can compare the output in the script: Echo $log    #输出的为变量的变量名 echo ${$log} #报错, where the error will be immediately after the exit, mainly to show you, we test the following several echo statements in advance to remove this statement or comment out. echo {!log} #成功 eval echo \$ $log #成功done


This article is from the "Red Mansions Dream" blog, please be sure to keep this source http://leidongya.blog.51cto.com/7375845/1577520

Indirect references to variables in the shell

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.