Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am using Slackware 12.2
I found a strange thing.
Code:
d=*Alg*
echo $d
And the result is a bunch of file names Alg_geom.myfile, my.Alg.mu ... etc
I want actually evaluate the variable d with the value *Alg* (five characters with 2 astars)
Same unexpected result returns with the following code:
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*"
echo "$d"
Hello colucix
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
"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*
echo "$d"
EDIT: a bash variable (in strict bash-speak, a parameter) only becomes an environment variable when it is exported.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.