LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 08-04-2011, 01:57 AM   #1
iamonly
LQ Newbie
 
Registered: Nov 2009
Distribution: Slackware 12.2.0
Posts: 13

Rep: Reputation: 0
Question 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?
 
Old 08-04-2011, 02:28 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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"
 
1 members found this post helpful.
Old 08-04-2011, 07:15 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
1 members found this post helpful.
Old 08-04-2011, 08:25 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by colucix View Post
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.

Last edited by catkin; 08-04-2011 at 08:31 AM.
 
1 members found this post helpful.
Old 08-04-2011, 09:35 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
1 members found this post helpful.
Old 08-04-2011, 11:21 AM   #6
iamonly
LQ Newbie
 
Registered: Nov 2009
Distribution: Slackware 12.2.0
Posts: 13

Original Poster
Rep: Reputation: 0
Wink 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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Environment variable tarunchawla Linux - Newbie 1 03-27-2010 11:19 PM
keep environment variable manelameur Ubuntu 13 02-05-2010 11:01 PM
Environment Variable linux_linux Linux - Newbie 4 03-20-2008 10:16 PM
Environment variable.. manomohan Programming 1 02-26-2007 11:27 PM
environment variable mimi Linux - Newbie 1 04-10-2002 08:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration