LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Can a environment variable = *Alg* ? (https://www.linuxquestions.org/questions/linux-general-1/can-a-environment-variable-%3D-%2Aalg%2A-895459/)

iamonly 08-04-2011 01:57 AM

Can a environment variable = *Alg* ?
 
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:
Code:

d=\*Alg\*
echo $d

Can someone help me out?

colucix 08-04-2011 02:28 AM

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"


David the H. 08-04-2011 07:15 AM

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.

catkin 08-04-2011 08:25 AM

Quote:

Originally Posted by colucix (Post 4433303)
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

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*
echo "$d"

EDIT: a bash variable (in strict bash-speak, a parameter) only becomes an environment variable when it is exported.

colucix 08-04-2011 09:35 AM

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
fileA  fileB  fileC
$ d=*D*
$ echo $d
*D*

Thank you for the clarification.

iamonly 08-04-2011 11:21 AM

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*
echo "$d"

Problem solved :)


All times are GMT -5. The time now is 01:01 PM.