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?

Chris Adams
  • 709
  • 3
  • 11
  • 18

2 Answers2

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}"
Dan Carley
  • 25,617
  • 5
  • 53
  • 70
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.

Karolis T.
  • 2,719
  • 7
  • 33
  • 45
  • 4
    backticks should be extracted with a hot needle and a pair of tweezers - **use $() instead** - http://mywiki.wooledge.org/BashFAQ/082 – Dennis Williamson Jul 07 '09 at 13:02