LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Put String/Chars Directly after a Variable? (https://www.linuxquestions.org/questions/programming-9/put-string-chars-directly-after-a-variable-945587/)

mrm5102 05-17-2012 03:16 PM

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

Ser Olmy 05-17-2012 03:17 PM

Code:

echo -e "${BOLD}Here is Bold Text${BOLDOFF}"

mrm5102 05-17-2012 03:18 PM

Wowzerz... LOL Thanks for the QUICK reply!!!

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


Thanks,
Matt

mrm5102 05-17-2012 03:19 PM

Sweet... Worked Like a charm!!!


Thanks Again,
Matt

David the H. 05-18-2012 09:39 AM

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"

mrm5102 05-18-2012 10:41 AM

Hey David, thanks for the reply...

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


Thanks Again,
Matt


All times are GMT -5. The time now is 08:55 AM.