Bash Append Text To a Variable

How do I append additional text a variable? For e.g., I’ve a vech set as follows:
vech=”London”
I’d like to add (append) “Bus” word to $vevh under UNIX scripting using bash?

You can simply use the following syntax:
vech="London"
vech="$vech Bus"
echo $vech

If you don’t want to update $vech, but just want to print it on screen, enter:

echo "$vech Bus"

You can also append another variable:

x="Mango"
y="Pickle"
x="$x $y"
echo "$x"

Finally:

x="Master"
# print 'Master' without a whitespace i.e. print Mastercard as a one word #
echo "${x}card"

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 10 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
10 comments… add one
  • [skqr] Feb 1, 2010 @ 20:34

    What you are doing there is redefining it.

    It’s completely useless.

    When appending stuff to a variable, the most likely scenario is doing it while on a loop or an “if” block.

    Redefining a variable in that context will just make it invisible once you get out of the loop or the “if” block.

    • Alex Oct 5, 2013 @ 3:32

      Actually it helped me, a lot xD well what i was looking for was on the last section but that’s ok. I wanted to automate g++ compilation, so i did:

      read -p "Enter cpp file name (without extension): " file
      echo "You selected ${file}.cpp"
      echo "The command to execute is:"
      echo "g++ -o $file ${file}.cpp -lSDL -lSDL_image -lSDL_ttf -lSDL_mixer"

      SO, not useless hehe

  • gimmer Mar 12, 2011 @ 14:32

    Isn’t that response just as useless? You don’t provide an alternative.

  • Kristof Wevers Apr 15, 2011 @ 15:42

    Reply on old post but useful for someone who stumbles upon this

    vech=”London”
    vech=$vech” Bus”
    echo $vech

  • Kristof Wevers Apr 15, 2011 @ 15:44

    Scratch that, should be

    vech=”London”
    echo $vech” Bus”

  • Boris Sep 14, 2011 @ 11:41

    Jee thanks for Your site. Google frequently drives me here.

  • randy Feb 4, 2013 @ 1:46

    Sample given above does not work if there is no space:

    vech=”Apple”
    vech=”$vechseed”
    echo $vech

    ^ ^ ^ Does not give you “Appleseed”

    • 🐧 nixCraft Feb 4, 2013 @ 8:12
      vech="Apple"
      vech="${vech}seed"
      echo "$vech"
      
      • randy Feb 4, 2013 @ 10:40

        Thanks nixCraft! … Your example works for me and I think completes all possible scenarios that might apply to the original question. I also observed now in my experiment, the following produce expected results:

        vech=”${vech}seed” # your original solution
        vech=”${vech}+:;{[seed” # embed special syntax character works fine
        vech=”${vech} seed” # embedding spaces also works fine
        vech=${vech}”seed” # shifting the first double quote to the right
        vech=${vech}” seed” # shifting the quote and embedding spaces
        vech=${vech}”+:;{[seed” # shifted double quote w/special character

  • Rogerio Madureira Aug 22, 2014 @ 16:42

    I had a bit different need: to read the content of differente files and append it to a variable. By the way “+=” did the trick.

    JS_F="www/js/jquery/plugins/Typeahead/0.10.4/typeahead.jquery.min.js
    www/js/lDdrF.js
    www/js/moment/2.8.1/moment.js
    www/js/jquery/plugins/bootstrap-datepicker/1.1.2/bootstrap-datepicker.js
    www/js/Core.js
    www/js/jquery/plugins/jquery.splitflap.js
    www/js/glb.js"
    #echo $JS_F
    
    for f in $JS_F
    do
    	JS_C+=$(<$f)
    done
    
    echo $JS_C
    

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum