how to append a value to a variable in shell script

16,719

Solution 1

It seems you have a Windows carriage return in your $BUILD_ID variable.

To check, try this command (the carriage return will be visible as a ^M) :

cat -A <<< "$BUILD_ID"

In your terminal,you can try this (to get the ^M char, use CTRL + V + M) :

$ BUILD_ID="585548979^M"
$ echo ${BUILD_ID}text

The result should be :

text48979

You can clean your variable with a Bash parameter substitution :

$ ID=${BUILD_ID%$'\r'}
$ echo ${ID}text
585548979text

Solution 2

Let's say we have a variablevar1=value. If I want to add some text to the beginning of the variable I can do:

var1="some text${var1}"

If I want to add some text to the end, it will be:

var1="${var1}some text"
Share:
16,719
sampath
Author by

sampath

Updated on June 05, 2022

Comments

  • sampath
    sampath almost 2 years

    I am getting a variable value from properties and I am able to access in sh file. but I am unable to append another value to that variable.

    Kindly suggest.

    $ echo "Build ID from properties:"$BUILD_ID
    Build ID from properties: abcd_v6_c1
    
    $ echo " num----------------" build_${BUILD_ID}.zip
    .zip---------------- build_abcd_v6_c1
    

    Kindly suggest how to append .zip value.