Can a environment variable = *Alg* ?
I am using Slackware 12.2
I found a strange thing. Code:
d=*Alg* I want actually evaluate the variable d with the value *Alg* (five characters with 2 astars) Same unexpected result returns with the following code: Code:
d=\*Alg\* |
The asterisk is interpreted by the shell as the globbing character and it is expanded accordingly, unless you embed it in double quotes:
Code:
d="*Alg*" |
Read these two pages now to fully understand your current situation:
http://mywiki.wooledge.org/glob http://mywiki.wooledge.org/Arguments Then when you have a bit of free time, sit down and read this entire document from end to end: http://mywiki.wooledge.org/BashGuide You'll be glad you did. |
Quote:
I thought that was right and was just going to say that single quotes and \-escapes would also prevent globbing but when I checked the GNU bash reference it turned out it is not true; the globing expansion in the OP happens not in the d=*Alg* assignment but in the echo $d Here's from the GNU bash reference (my bolding): "A variable may be assigned to by a statement of the form name=[value] If value is not given, the variable is assigned the null string. All values undergo tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and quote removal (detailed below). [snip] Word splitting is not performed, with the exception of "$@" as explained below. Filename expansion is not performed". So the cleanest fix for the OP code is: Code:
d=*Alg* |
Hello catkin! :)
Of course, the filename expansion occurs when the variable is referenced. I would add that if the current directory does not contain filenames that match the literal string (the part besides the asterisks) the expansion doesn't occur at all and the asterisks are preserved even without the double quotes: Code:
$ ls |
thanks
Thank you for all your replies: colucix, David the H., catkin.
Especially thanks go to catkin, for the following codes saved me. Code:
d=*Alg* |
All times are GMT -5. The time now is 01:01 PM. |