1

When I try to echo or print a string, it prints fine.

This is the string.

IP_NET_TP0                                                                               _

The structure is IP_NET_TP0, and 9 tabs (\t), then 7 spaces and 1 underscore at the end.

Ok, if you put that into a variable, let's say var1 and you print it as follows it works great.

echo $var1 
echo "$var1"

BUT, if you add extra texts to it, for example, adding quotes, or adding a letter, it removes 1 space from the chain.

echo \"$var1\" 
echo "'"$var1"'"
echo "a" $var1

WHY?! .. I have tried printing the variable with print, or like this ${var1}, and the output if the same, with 1 space less.

The following shows, the first one when printed with something else, and the second when printed alone.

IP_NET_TP0                                                                             _
IP_NET_TP0                                                                               _

Any help will be great.

2
  • Try with printf (which is also a command, even a bash builtin) Commented Nov 13, 2013 at 23:56
  • It was a good one but, the same results, I am using printf "\"%s\"" "${mat[c][0]}" I changed my var name to mat[][], but the behavior is the same. Commented Nov 14, 2013 at 0:16

3 Answers 3

3

Perhaps you are instantiating the variable incorrectly. When I declare it like this var1="IP_NET_TP0" or var1=IP_NET_TP0 printing it the ways you did works fine. Are you using bash or a different type of shell? There might also be something weird in your .bash_profile or .bashrc causing this.

Sign up to request clarification or add additional context in comments.

6 Comments

I am using KSH ..., and I am still trying to make this thing to work correctly
I'm not getting this problem when instantiating the var1 the way I mentioned above. How are you declaring the variable with this value? That may be the error. If that isn't it, it might be something with the way your shell is configured.
The var1 is coming from a sql query, and it is coming with all those tabs and spaces, as I said, if I just print it, it works, but if I add something else to what I am going to print, it fails the way I described.
Try statically grabbing that value just like you copied and pasted it into your question, assign it to a variable and see if the same thing happens. If not, then something must be incorrect when assigning the variable to the sql query result.
same thing my friend... What I just noticed actually typing the string is, that my first tab is shorter then the rest of tabs, let me try to overcome that and see what happens.
|
1

you didn't write the echo correctly when adding the extra patterne

echo \"$var1\" -> echo "\"$var1\""
echo "'"$var1"'" -> echo "'$var1'"
echo "a" $var1 -> echo "a $var1"  #if space is needed between a and the var1

also try to use ${var1} to avoid confusion when like this

var="1 x 1 = "
var1="Something"
echo "$var1"
echo "${var}1"
echo "${var1}"

3 Comments

All the options are well written, they are interpreted in line correctly, and those are just some of the different prints that I have tried. The results are the same with all the echos, prints, printfs that I have tried, between quotes, without them, with {}, etc, haha I don't know what else to do.
Issue fixed by using expand.
are you sur it is missing ? I test here and character are present but due to \t seem missing. Check with a |wc -c to see how much char
1

Issue fixed, as the string was built converting spaces to tabs, there are tabs with different sizes! ... causing probably an interpretation issue when printed, what I did was, before printing the variable, convert all the tabs to spaces so the size won't change any more like this var2=echo $var1 | expand` ... and done, the size is now fixed and it is not going to change any more.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.