#!/bin/bash
v="test"
result=`grep 'Dir=' /home/xtend/Videos/file.ini | cut -d'=' -f 2`;
foo="$result"
foo+="$v"
echo $foo

answer expecting(/home/file/test)
answer getting(teste/file)

(In "/home/xtend/Videos/file.ini" "Dir=/home/file/") ...

how it is outputing like this?

Dani AI

Generated

Most likely cause: the value you grep from the INI file contains a Windows carriage-return (CR, 0x0D). A string like /home/file/\rtest will display as teste/file/ because \r returns the cursor to the start of the line and the appended test overwrites the beginning. So this is not a concatenation-order bug — it’s a control character in the value.

Quick checks you can run at the prompt to confirm the CR is present:

printf '%s' "$result" | hexdump -C
# or inspect the file for ^M
cat -v /home/xtend/Videos/file.ini

If you see 0d 0a bytes or ^M at line ends, fix it before concatenating. Fix options:

  • Convert the file to Unix line endings (e.g., dos2unix).
  • Strip CRs when reading the value (example uses parameter expansion to remove \r).
  • Or pipe through tr -d '\r' when extracting.

Example of cleaning the variable and printing safely:

result=${result//$'\r'/}
printf '%s\n' "${result}${v}"

As suggested, try commands interactively to see how each step behaves. Also quote variable expansions when printing (use printf or echo with quotes) to avoid word-splitting and surprises. For ’s case (Dir=/home/file/), removing the \r will make the appended test appear as /home/file/test as expected.

Be sure to try each command at the bash prompt to see how it works.

Also, try the older string concatention method. Example:
foo="$result $v"
Or forget v and foo then write:
foo="$result test"

commented: (I have tried it using awk also but the result is same)result=`awk '$0 ~ /Dir=/{print}' $xv | cut -d'=' -f 2`; +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.