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!

39 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.

  22. on 03 Sep 2010 at 6:27 pm Lou

    Wow.. 3 years old and still helping people. Good tip.

  23. on 14 Oct 2010 at 5:20 am Alan

    Thanks! Google throws up tons of useless answers to this problem. You just saved me half an hour.

  24. on 15 Oct 2010 at 6:55 pm Eric

    thanks. Cool navigation menu btw

  25. on 26 Jan 2011 at 12:10 pm Lyneham

    Hi

    I have a heap of duplicate files (avi and jpegs etc).

    File names are often sequencenumber.avi, or sequencenumber.jpg

    The parent directory or a directory in the path contains a better description of what the file is.. eg 2008 Files off Camera/trip to paris/R0001.avi (many files look like this.

    In those cases I would want the file name to be changed to trip to paris 0001.avi etc.

    any idea how I might do this?

    L

  26. on 11 Feb 2011 at 2:27 am Juha

    I would do it something like this. Modify it to meet your requirements. Script does not check the input but it could be done with grep and with the same regural expression what the Filename is using. You can make it more efficient by giving the folder with a parameters. But here is a small help.

    FOLDER=`pwd`
    cd $FOLDER
    TRIPNAME=`basename ${FOLDER}`

    for i in *.avi;do
    if [ -f $i ]; then
    FILENAME=`echo $i |sed ‘s_R\([0-9]*.avi\)_\1_’`
    echo “mv $i ${TRIPNAME}${FILENAME}”
    mv $i ${TRIPNAME}${FILENAME}
    fi
    done

  27. on 11 Feb 2011 at 2:30 am Juha

    And for the catenation I used it like this. Tarstring is used as a part of larger command string.

    for i in ../package/*.jpg;do
    if [ -f $i ]; then
    filename=`basename ${i}`
    tarstring=${tarstring}” “${filename}
    fi
    done

  28. on 23 Feb 2011 at 9:23 am Helmut

    Hi!
    How do I get the result of the first line and the second line concatenated into one line and added to a text file, not onto two lines like this does it?

    ls ./$aa.*|wc -l >> crds.txt;
    echo “|”$aa >> crds.txt

    instead of
    2347
    |agency

    it should write
    2347|agency

    Thanks!
    Helmut

  29. on 07 Mar 2011 at 6:59 pm Ben

    Your example works in the case that both variables being concatenated need to be dereferenced at the time of assignment. In the case that you’d like to concatenate variables and not dereference the values. For example, if you are building up the text of a command to be run in the future

    $format= “$1″
    $date_cmd= $(echo ‘date ‘ “+$format”)

    Now you can run $date_cmd as a command and it will evaluate each time.

  30. on 13 Aug 2011 at 10:15 pm arbit

    thank you very much :D

  31. on 26 Aug 2011 at 4:13 pm P. Terson

    yay

  32. on 11 Oct 2011 at 10:14 am vero

    cheers! ^^

  33. [...] I didn’t even know why you need to use ‘export’. I didn’t know how to concatenate strings in a bash variable. Or, even how to run a command and capture the output in a variable. I also didn’t know how [...]

  34. on 27 Jun 2012 at 8:48 pm ggk

    Thanks for the tip..

  35. […] concatenate strings in bash » from the desk of stinkpot – Hi. I have a heap of duplicate files (avi and jpegs etc). File names are often sequencenumber.avi, or sequencenumber.jpg. The parent directory or a directory in the … […]

  36. […] concatenate strings in bash » from the desk of stinkpot – Hi. I have a heap of duplicate files (avi and jpegs etc). File names are often sequencenumber.avi, or sequencenumber.jpg. The parent directory or a directory in … […]

  37. […] concatenate strings in bash » from the desk of stinkpot – 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 … […]

  38. on 26 Apr 2017 at 8:40 pm fix iphone 7 plus glass

    Why visitors still make use of to read news papers when in this
    technological world everything is accessible
    on web?

  39. on 18 Jul 2017 at 5:38 am health insurance

    I pay a quick visit day-to-day a few websites and sites to read posts, however this webpage gives quality based content.

Did I get this wrong? Let me know!

Trackback URI | Comments RSS

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