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”


Bookmark and Share

if that was helpful ...

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

21 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

  12. on 09 Dec 2008 at 7:46 am Caio Moritz Ronchi

    Great tip, that will be easy to remember.

  13. on 30 Jan 2009 at 2:25 am John

    May you live to be a thousand years old. (Thank you for the help.)

  14. on 12 Mar 2009 at 5:19 am edi

    i want to use for loop for assign:

    for i in 0 1 2 3
    do
    id$i=`cat file | grep list$i`
    done

    then:
    cat file | grep list$i - work well
    but assign bring error
    -sh: id0=: not found

  15. on 31 Mar 2009 at 9:27 am barton

    edi,

    You need to typeset id$i like this:

    typeset id$i=”cat file | grep list$i”

  16. on 25 Aug 2009 at 12:14 pm Stephen

    Thank you for your very helpfup info on joining strings in shell scripts.

  17. on 09 Oct 2009 at 3:43 pm lazevedo

    dammit!
    one of the most useful things i’ve read today!

  18. on 01 Dec 2009 at 6:20 pm electroman

    Here is where you absolutly need this tip…$k=”${j}bar”

    copy/paste to /tmp/test.sh

    =========================================

    #!/bin/bash

    # test.sh

    APPNAME=mediawiki
    clear
    cat << EOF
    Here’s what we get….

    \$APPNAME=mediawiki

    We want to concantinate the value of \$APPNAME and user to be mediawikiuser

    MYSQLUSER=\${APPNAME}user << subs the var value & concantenates correctly

    MYSQLUSER=${APPNAME}user << this is what we want

    MYSQLUSER=\$APPNAMEuser << this doesn’t work, we’re subbing the var name \$APPNAME

    MYSQLUSER=$APPNAMEuser < /tmp/test_2.sh << EOF

    #!/bin/bash

    # test_2.sh

    APPNAME=mediawiki
    clear
    cat << EOF

    We want to concantinate the value of \$APPNAME and user to be mediawikiuser

    MYSQLUSER=${APPNAME}user < /tmp/test_2.sh << EOF

    #!/bin/bash

    # test_2.sh

    APPNAME=mediawiki
    clear

    echo “MYSQLUSER=${APPNAME}user << this is what we want”
    echo “MYSQLUSER=\${APPNAME}user << this is what’s between the cat EOF’s”

    MYSQLUSER=\${APPNAME}user # Assign \${APPNAME}user to MYSQLUSER

    echo -e “\n \$MYSQLUSER << the result of MYSQLUSER when we run /tmp/test_2.sh\n”

    echo -e “\nCool…!!\n\n”

    exit

    EOF

    chmod 770 /tmp/test_2.sh
    read -p “Press return to run /tmp/test_2.sh” input

    /tmp/test_2.sh

    echo -e “\n\n Here’s the contents of /tmp/test_2.sh as seen above…\n\n”

    cat /tmp/test_2.sh

    cat << EOF

    So what we have done here is to generate a script form a script and substituted var values and var names.
    Each with different results…when we pipe through cat

    Interesting…

    EOF
    exit

    =========================================

  19. on 03 Mar 2010 at 8:18 am Devika

    I want to run a program in which i will get say for eg; 6 output.I want each output in different files.

    My output file should be file1.txt
    file2.txt
    file3.txt
    Each time i want the output file created automatically with the nos alone changing.

  20. on 30 Mar 2010 at 6:48 pm Aleena

    Hi
    i am facing two problems and I guess this is the best place to get help from.

    i am using bash and need to write a bash script.

    Problem-1
    I have two file t1.txt and t2.txt. each with 100 lines. I am trying to make 100 file that each line of file t2.txt is concatenated with line number 1 or t1.txt and file is named as o1.txt. then each line of t2.txt is concatenated with 2nd line of t1.txt and saved as 02.txt and so on till all lines in t1.txt are exhausted.
    problem-2
    I have a number of CDIR in format of 12.458.0.0/24 etc.
    I need a bash script that do following:-
    delete all character on right of / in each line.
    if all character on left side of / till it hits a . while deleting towards left.
    if character on left of . is a 0 then delete both 0 and “.”.
    I guess its a challenging question. but i am looking forward for a guidance.
    thanks in advance

  21. on 23 Jul 2010 at 5:39 am Robert

    Thank you so much for this. Saved me hours.

Did I get this wrong? Let me know!

Trackback URI | Comments RSS

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