Sunday, May 29, 2022
HomeNetworkingConcatenating strings and utilizing += in bash

Concatenating strings and utilizing += in bash


It is fairly straightforward to get bash to concatenate strings and do simple arithmetic on Linux, however there are a variety of choices so that you can use. This submit focusses on concatenating strings, but additionally exhibits how one of many operators (+=) additionally performs a major function in incrementing numbers.

Concatenating strings

Normally, the one time that you just’d need to concatenate strings on Linux is when one string is already outlined and also you need to add extra to it. For instance, in case you have a script that greets the individual working it, you would possibly arrange a string within the script to arrange the greeting after which add the individual’s username or title earlier than displaying it.

On this first instance, we create a welcome string in a script after which pull the consumer’s first title from the /and so forth/passwd file and embody it within the displayed textual content.

#!/bin/bash
greeting="Welcome! Let's get began along with your annual interview"
title=`grep ^$USER /and so forth/passwd | awk -F: '{print $5}' | awk '{print $1}'`
greet="$greeting, $title"

echo $greet

When the script is run, it would begin like this:

$ ./greet
Welcome! Let's get began along with your annual interview, Sandra

The greeting was composed by merely together with each the predefined greeting and the consumer’s first title in a variable named “greet”. Whether or not you create one string from two or just use each in an echo command makes little distinction. We may have simply displayed the greeting like this as an alternative of making separate $greeting and $greet variables:

echo "Welcome! Let's get began along with your annual interview, $title"

You too can add to an current greeting with out creating a further variable as in these three command sequences. Discover how, within the first one, the $greet variable is included within the command that redefines it.

$ greet="Welcome! Let's get began along with your annual interview"
$ greet="${greet}, $USER"
$ echo $greet
Welcome! Let's get began along with your annual interview, shs

The next command, utilizing printf, has the identical impact.

$ greet="Welcome! Let's get began along with your annual interview, "
$ greet=$(printf "%s $USER" "$greet")
$ echo $greet
Welcome! Let's get began along with your annual interview, shs

This final method combines three variables to create the greeting and makes use of the total username from the /and so forth/passwd file.

$ greet="Welcome! Let's get began along with your annual interview"
$ punct=", "
$ goal=`grep ^$USER /and so forth/passwd | awk -F: '{print $5}'`
$ echo "$greet$punct$goal"
Welcome! Let's get began along with your annual interview, Sandra Henry-Stocker

The += sequence proven within the subsequent instance appends a reputation to the occasion variable:

$ occasion="Blissful Birthday"
$ occasion+=", Amanda"
$ echo $occasion
Blissful Birthday, Amanda

Incrementing numbers

This += sequence can be utilized to increment numbers. Within the examples beneath, we arrange a variable, add 1 to its worth after which add 14. Discover how the ++ (add 1) and += (add specified quantity) sequences differ.

$ x=10
$ ((++x))
$ echo $x
11
$ ((x+=14))
$ echo $x
25

NOTE: Utilizing += with out the double parentheses does one thing altogether completely different. It treats the digits as strings and concatenates them.

$ y=12; echo $y
12
$ y+=34; echo $y
1234

To make use of the += sequence so as to add to an array, you are able to do one thing like what you see beneath. First, we outline $x as proven beneath, creating the array within the course of.

$ x=25
$ echo ${x[@]} 25

Then we are able to add a second aspect utilizing the += sequence like this:

$ x+=(34)

After we show the array once more, we see it now has two components:

$ echo ${x[@]}
25 34

You possibly can show the scale of an array with a command like this:

$ echo ${#x[@]}
2

Wrap-Up

Manipulating strings by defining separate parts after which utilizing them collectively could make utilizing the strings in scripts extra handy when solely small portion of the textual content is prone to fluctuate. The += sequence supplies helpful choices each in manipulating each strings and numbers, however the latter can contain math in addition to string concatenation. It is good to know which one to count on.

Be a part of the Community World communities on Fb and LinkedIn to touch upon subjects which might be prime of thoughts.

Copyright © 2022 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments