LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2008, 03:26 AM   #1
lutian
LQ Newbie
 
Registered: Apr 2006
Posts: 5

Rep: Reputation: 0
how to deal with character "-",it is a part of variable name


I meet a issue,someone define project name with "-" character,

how to deal with "-" in bash scripts,

"-" as part of variable name
set aa-bb="AAAA'
echo ${aa-bb}

Last edited by lutian; 11-12-2008 at 03:30 AM.
 
Old 11-12-2008, 06:21 AM   #2
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
Well, first off, just to be sure, have you noticed that you have mismatched quotation marks around the variable value in your example above?
 
Old 11-12-2008, 01:13 PM   #3
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

it's not possible to use "-" in variable names. See "man bash":
Quote:
name
A word consisting only of alphanumeric characters and underscores, and beginning
with an alphabetic character or an underscore. Also referred to as an identi-
fier.
Jan
 
Old 11-12-2008, 06:48 PM   #4
lutian
LQ Newbie
 
Registered: Apr 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jan61 View Post
Moin,

it's not possible to use "-" in variable names. See "man bash":


Jan


yes,vaiable names cannot use "-",

In my office, someone define a project name with "-" on cvs,

in my scripts program,how to deal with a project name have "-" character,


Thanks

Last edited by lutian; 11-12-2008 at 06:53 PM.
 
Old 11-12-2008, 07:03 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by lutian View Post
yes,vaiable names cannot use "-",

In my office, someone define a project name with "-" on cvs,

in my scripts program,how to deal with a project name have "-" character,


Thanks
Change the language to, say, Perl and use hash keys as variable names -
hash key can be any string.
 
Old 11-12-2008, 07:04 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
That doesn't seem like it should be any problem. In what way doesn't it work? Your example used "-" in a variable name, not in anything passed as a command-line argument.
ta0kira
 
Old 11-12-2008, 08:00 PM   #7
lutian
LQ Newbie
 
Registered: Apr 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ta0kira View Post
That doesn't seem like it should be any problem. In what way doesn't it work? Your example used "-" in a variable name, not in anything passed as a command-line argument.
ta0kira
When I put the project name to variable name,the scripts dosenot work.
 
Old 11-12-2008, 10:01 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Can you post it? You example doesn't reflect that.
ta0kira
 
Old 11-13-2008, 02:42 AM   #9
lutian
LQ Newbie
 
Registered: Apr 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ta0kira View Post
Can you post it? You example doesn't reflect that.
ta0kira

Have been solved,

The root cause is variable name using "-",

is not variable with "-",

I define a new variable name without "_",that is OK now.


Thanks veryone.
 
Old 11-13-2008, 01:45 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by lutian View Post
Have been solved,

The root cause is variable name using "-",

is not variable with "-",

I define a new variable name without "_",that is OK now.


Thanks veryone.
It's not OK and will never be with your approach.

Suppose you have two projects:

foo-bar
foo_bar
.

How are you going to distinguish them ?
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to deal character "-",it is a part of variable name lutian Linux - Newbie 1 11-12-2008 02:53 PM
LXer: Response to "Why Wal-Mart Linux PC Is A Bad Deal" article LXer Syndicated Linux News 0 11-15-2007 04:50 PM
How to deal with filenames starting with "-" or "--"? [KIA]aze Linux - Newbie 2 07-08-2007 01:31 AM
"Broken" envirnment variable (MANPATH) and "/etc/profile.d" question. ErV Slackware 3 03-20-2007 09:42 AM
What's the deal with the urpmi "contrib" sources? adamis Mandriva 6 02-01-2004 12:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:06 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