Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm used to being able to pass variables inside strings in ruby, like so

"message in double quotes #{expression_or_variable_to_run}"

What's the equivalent in bash, for really quick scripting?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

Is this what you're after?

#!/bin/bash

# Source hostname from command.
echo "Hostname is $(hostname)"

# Set hostname as string.
HOSTNAME="somestring"
echo "Hostname is ${HOSTNAME}"
share|improve this answer
    
Beat me by that much. –  Dennis Williamson Jul 7 '09 at 13:04
2  
However, $() is not "sourcing" - it's a subshell. –  Dennis Williamson Jul 7 '09 at 13:06
    
Ah, this is -exactly- what need, thanks! –  Chris Adams Jul 7 '09 at 14:26
    
I didn't mean source in a Bash '. ' sense. Just that the command was the source of the string. Semantics ;) –  Dan Carley Jul 8 '09 at 10:08

Maybe this will help you understand some basics:

#!/bin/bash
VARIABLE="is"
echo "the server $VARIABLE `hostname`"

Variables are defined without $, referenced with $. Shell commands can be executed within `` quotes.

share|improve this answer
4  
backticks should be extracted with a hot needle and a pair of tweezers - use $() instead - mywiki.wooledge.org/BashFAQ/082 –  Dennis Williamson Jul 7 '09 at 13:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.