LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-04-2010, 04:24 AM   #1
laki47
Member
 
Registered: Feb 2009
Posts: 33

Rep: Reputation: 15
Concatenate two variables with underscore - question


Hi,

I have question regarding concatenation of two variables with underscore.

i.e. (bourne shell)

Code:
# var1=123
# var2=456
# echo $var1_$var2
sh: var1_: Parameter not set.
# echo $var1"_"$var2
# 123_456
My question is: What is the issue with underscore when I put it witout quotes? Why is it automatically concatenated to variable name (sh: var1_: Parameter not set.)?

Thanks!

Last edited by laki47; 06-04-2010 at 04:46 AM.
 
Old 06-04-2010, 04:35 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Quote:
What is the issue with underscore
The issue would be that it thinks the underscore is part of the variable name.
eg. think about
Code:
var=10

echo $varx
You would not expect this to work.
You can however use braces to clarify variable names:
Code:
var=10

echo ${var}x
 
Old 06-04-2010, 04:44 AM   #3
laki47
Member
 
Registered: Feb 2009
Posts: 33

Original Poster
Rep: Reputation: 15
But why will this work then:

Code:
# echo $var1-$var2
# 123-456
Why in this case the "-" is not concatenated?

Last edited by laki47; 06-04-2010 at 04:46 AM.
 
Old 06-04-2010, 05:02 AM   #4
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
In the bash man page is a section on definitions.
Quote:
blank A space or tab.

word A sequence of characters considered as a single unit by the shell. Also known as a token.

name A word consisting only of alphanumeric characters and underscores, and beginning with an alphabetic character or an underscore. Also referred to as an identifier.

metacharacter A character that, when unquoted, separates words. One of the following: | & ; ( ) < > space tab

control operator A token that performs a control function. It is one of the following symbols: || & && ; ;; ( ) | |& <newline>
So the underscore is considered along with alphanumeric characters as being part of a "name", while others, such as the hyphen, are not.

By the way, here's a small trick you can use so that the underscore is only inserted if the second variable exists. Of course, it can only be used on shells that support this parameter substitution.

Code:
echo "${var1}${var2:+_}${var2}"

or more consisely:

echo "${var1}${var2:+_$var2}"

Last edited by David the H.; 06-04-2010 at 05:12 AM. Reason: formatting + an inadvertent omission
 
  


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
Bash script: concatenate variables with period character the182guy Linux - Software 4 12-28-2009 01:41 PM
sed, replacing underscore with whitespace fjkum Programming 3 10-31-2007 12:09 AM
C: fprintf with underscore jhwilliams Programming 8 08-25-2007 12:08 AM
deleting an underscore Post Modern Programming 2 11-27-2005 05:35 AM
Question about variables setianusa Programming 3 05-05-2005 06:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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