3

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?

7

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}"
  • 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
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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