An issue that cannot be referenced after a variable in the shell is defined as an asterisk (*)

Source: Internet
Author: User
Tags syslog

In the process of writing a shell script, it is sometimes necessary to use some variable values are defined as (*) variables, but when we try to reference this variable, bash has the default to replace (*) the list of all the filenames in the current directory, as follows:

[Email protected]_102 ~]# a=*[[email protected]_102 ~]# echo $aanaconda-ks.cfg install.log Install.log.syslog[[email protected]_102 ~]# ls anaconda-ks.cfg install.log install.log.syslog

At this point we can consider a question: where is the (*) step being replaced with the file list below the current directory: Is it replaced at the first step, when the variable is assigned, or when the Echo variable value is replaced?

The truth is this:

1, when the variable is copied, bash will directly assign (*) to the variable A;

2, but in the second step of referencing the variable, bash defaults to replacing (*) with a list of all the files in the current directory, so you can experiment with this:

[Email protected]_102 ~]# echo *anaconda-ks.cfg install.log install.log.syslog

But how to remove the value of the variable A, this time the variable refers to the function of the quotation mark is reflected:

When we reference a variable, there is no quotation mark, single quotation mark, double quotation mark difference: ()

[Email protected]_102 ~]# echo "$a" #将引号里面的变量替换成相对应变量值 *[[email protected]_102 ~]# echo ' $a ' $a #将引号里面的字符统统不做转义, all output by string

The questions follow, and when I want to use the IF statement in a shell script to determine if the variable value of a variable is (*), there is an error:

[[Email protected]_102 ~]# [["$a"-eq *]] && echo AA | | echo Bb-bash: [[: *: Syntax Error:operand expected (Error token is "*") BB

So think about it, will it be (*) not the reason for double quotation marks:

[[Email protected]_102 ~]# [["$a"-eq "*"]] && echo AA | | echo Bb-bash: [[: *: Syntax Error:operand expected (Error token is "*") BB

OK, no, the-eq (= =) to try:

[[Email protected]_102 ~]# [["$a" = = "*"]] && echo AA | | echo bbaa[[email protected]_102 ~]# [["$a" = = *]] && echo AA | | echo bbaa[[email protected]_102 ~]# [[$a = = "*"]] && echo AA | | echo bbaa[[email protected]_102 ~]# [[$a = = *]] && echo AA | | Echo Bbaa

People may wonder why the last one is not quoted is also possible, here we can do another experiment to see:

We assign a list of all filenames under the current directory as variable values to C

[Email protected]_102 ~]# echo $aanaconda-ks.cfg install.log install.log.syslog[[email protected]_102 ~]# c= " Anaconda-ks.cfg install.log install.log.syslog "[Email protected]_102 ~]# [[" $c "= = *]] && echo AA | | echo bbaa[[email protected]_102 ~]# [["$c" = = "*"]] && echo AA | | Echo bbbb

This time we can see that when both sides are not double quotes, (= =) on both sides of the current directory is a list of all the files as a variable value to compare, and finally is equal;

But when the side of the quotation marks, side does not add why also equal, temporarily also cannot explain, if you know why, trouble in the comment area below to inform, greatly appreciated.

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

An issue that cannot be referenced after a variable in the shell is defined as an asterisk (*)

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.