Results 1 to 5 of 5

Thread: Use a variable within quotation marks: "$variable"

  1. #1
    Join Date
    Apr 2006
    Location
    UK
    Beans
    6,646
    Distro
    Ubuntu 12.04 Precise Pangolin

    Use a variable within quotation marks: "$variable"

    I'm writing a simple (-ish) bash script to automate variant annotation for genetic sequencing data.

    I need to put a variable within quotation marks - see below.

    Code:
    gatkname=$vcfname".gatk"
    gatktable="g"$vcfname
    
    vtools select variant --samples 'sample_name="$gatkname"' -t $gatktable
    Just to confirm, the entries work when done manually:
    Code:
    vtools select variant --samples 'sample_name = "20AA02205.gatk"' -t g20AA02205
    I can't find any way for the variable to be recognised as text within quotation marks. I'm sure this must be easy - but googling hasn't helped.

  2. #2
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,292
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Use a variable within quotation marks: "$variable"

    Single quotes mean everything in-between is interpreted literally:

    Code:
    bash-3.2$ foo=bar
    bash-3.2$ echo '$foo'
    $foo
    bash-3.2$ echo "$foo"
    bar

    You want

    Code:
    vtools select variant --samples "sample_name=\"$gatkname\"" -t $gatktable
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Apr 2006
    Location
    UK
    Beans
    6,646
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Use a variable within quotation marks: "$variable"

    Thanks.
    I knew it was simple. I was confused by the difference between ' and "
    Seems obvious now.
    I'm just about to run my script... Will be a few hours before I get results - but at least I'm on track.

  4. #4
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: Use a variable within quotation marks: "$variable"

    you can also escape the $:
    Code:
    echo "\$foo"

  5. #5
    Join Date
    Feb 2008
    Location
    Mine goes to 11
    Beans
    3,656

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •