LinuxQuestions.org
Help answer threads with 0 replies.
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 05-17-2012, 03:16 PM   #1
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 165

Rep: Reputation: 3
Put String/Chars Directly after a Variable?


Hello All,

This might just be a simple yes or no answer to this if anyone could help me out.

Is it possible in a Bash Script (or any Shell Script), to put a string right after a variable in an echo statement, without the "string" being considered as part of the variable..?

I was trying to simplify using Bold text in a Script...

For Example:
Code:
#!/bin/bash


BOLD="\033[1m"
BOLDOFF="\033[0m"

echo -e "$BOLDHere is Bold Text$BOLDOFF"
Is there any, special character for example, that I could put between $BOLD and "Here" that would separate the two without leaving whitespace?


Any thoughts would be much appreciated!

Thanks in Advance,
Matt
 
Old 05-17-2012, 03:17 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,333

Rep: Reputation: Disabled
Code:
echo -e "${BOLD}Here is Bold Text${BOLDOFF}"
 
1 members found this post helpful.
Old 05-17-2012, 03:18 PM   #3
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 165

Original Poster
Rep: Reputation: 3
Wowzerz... LOL Thanks for the QUICK reply!!!

Awesome I'll give that a try... Thanks!


Thanks,
Matt
 
Old 05-17-2012, 03:19 PM   #4
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 165

Original Poster
Rep: Reputation: 3
Sweet... Worked Like a charm!!!


Thanks Again,
Matt
 
Old 05-18-2012, 09:39 AM   #5
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
The shell reads a variable name as a contiguous string of letters, digits, and the underscore (except that it can't start with a digit). The first character that is illegal in a variable name terminates it.

When you need to combine a variable and a text string like this, the full bracketed form of the variable expansion is the generally accepted way to go about it.

Another possibility, however, is to quote each part separately.

Code:
echo -e "$BOLD""Here is Bold Text$BOLDOFF"
Due to the way the shell parses the line, each quoted section is parsed separately, then concatted back together to form the final string.


Yet another option, and perhaps the cleanest, is to use printf instead:

Code:
printf "$BOLD%s$BOLDOFF\n" "Here is Bold Text"
 
1 members found this post helpful.
Old 05-18-2012, 10:41 AM   #6
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 165

Original Poster
Rep: Reputation: 3
Hey David, thanks for the reply...

Cool, thanks for the explanation, and some other options to go with!


Thanks Again,
Matt
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed: replace regexp w/ variable #s of chars with the same # of (diff.) chars? kmkocot Linux - Newbie 6 11-18-2011 05:36 AM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
[SOLVED] C - How to put a specific arbitrary part of a string into it's own string? golmschenk Programming 9 04-19-2010 08:27 PM
C string as an array of chars and as a pointer to char Alien_Hominid Programming 16 05-18-2009 08:22 AM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 05:21 PM

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

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