Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 7 down vote accepted

Is this what you're after?


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

# Set hostname as string.
echo "Hostname is ${HOSTNAME}"
share|improve this answer
Beat me by that much. – Dennis Williamson Jul 7 '09 at 13:04
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:

echo "the server $VARIABLE `hostname`"

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

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

Your Answer


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.