Feed on
Posts
Comments

let’s say you’ve got:

$j=”foo”

and you’d like to tack on its good partner ‘bar’. then, use the dollar sign and curly braces to delineate variable from string:

$k=”${j}bar”

Stumble it!

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

11 Responses to “concatenate strings in bash”

  1. on 17 Jul 2007 at 6:59 am bela

    Thank a lot

  2. on 23 Jul 2007 at 11:26 am anyonymous

    holy crap, thanks a lot man. a search on Google reveals nothing but extremely bad examples and explain nothing.

  3. on 02 Aug 2007 at 10:45 am Arun Arunachalam

    Bulls eye.. I just got exactly what I needed.

  4. on 24 Sep 2007 at 2:01 pm Ben

    I think it should be like this:
    k=“${j}bar”

  5. on 24 Sep 2007 at 2:24 pm Lawrence David

    nice catch ben; duly updated!

  6. on 31 Oct 2007 at 11:01 am Sam

    Thanks!

  7. on 16 Nov 2007 at 6:33 pm Amy

    thanks!! Very helpful!!

  8. on 27 Dec 2007 at 8:58 am spook

    IMHO a simpler solution would be:

    #!/bin/bash
    j=”foo”
    k=”bar”
    concatenation=”$j$k”
    echo j: $j
    echo k: $k
    echo j+k: $concatenation

    This will produce:
    j: foo
    k: bar
    j+k: foobar

  9. on 04 Feb 2008 at 5:36 pm Robert

    Outstanding!

  10. on 19 Mar 2008 at 11:32 pm xer0

    Just leave out those citation marks…

    [jakob@kage ~]$ j=”foo”
    [jakob@kage ~]$ k=”bar”
    [jakob@kage ~]$ concatenation=”$j$k”
    [jakob@kage ~]$ echo $concatenation
    ””foo””bar””

    ^^

  11. on 03 Nov 2008 at 1:21 pm rmiller

    you don’t get into trouble until you stat with
    complicated file names -

    PTH=”/work/dir/test”
    DTE=”2008/10/31″

    FILENAME=${PTH}/${DTE}_suffix.out

    this practice guarantees it will work

Did I get this wrong? Let me know!

Trackback URI | Comments RSS

More blogs about http://desk.stinkpot.org:8080/tricks.