Could any one tell me why the below echo command seems to behave inversely? Thanks.
Quote:
[justdemon ~]$ t="1\n"; t=$(echo -e $t); echo -e $t
1
[justdemon ~]$ t="1\n"; t=$(echo $t); echo -e $t
1
[justdemon ~]$
Printable View
Could any one tell me why the below echo command seems to behave inversely? Thanks.
Quote:
[justdemon ~]$ t="1\n"; t=$(echo -e $t); echo -e $t
1
[justdemon ~]$ t="1\n"; t=$(echo $t); echo -e $t
1
[justdemon ~]$
what do you mean by inversely??
what answer are you expecting?
let me explain;
take this one first:
t="1\n"; t=$(echo -e $t); echo -e $t
split it away;
$ t="1\n" (it will assign the value to "t")
$ t=$(echo -e $t)
in this you have mentioned -e flag so it will enable interpretation of the backslash-escaped characters, in our case it is "\n"(new line character).
so t will be assigned "1" after this step.
at last, $ echo -e $t
it will print the value of t (this time there is no backslash-escaped characters ).
Now consider second one,
t="1\n"; t=$(echo $t); echo -e $t
$ t="1\n" (it will assign the value to "t")
$ t=$(echo $t)
here you have not mentioned -e flag so echo will not enable interpretation of the backslash-escaped characters, and t will be assigned "1\n" only.
at last, $ echo -e $t
it will print the value of t (this time it will enable interpretation of the backslash-escaped characters, so it will print "1" + new line).
Hope you have got you answer.
Dear patelhemal2210,
Thanks very much for your detailed explanation.
It looks like I got incorrect idea of -e option.
Best Regards,
justdemon
you are always welcome.:-)
I found that I still have question on this.
Even I use the following command, I can't put the "newline" character into the variable.
How to do to assign a newline included in the original variable into a new variable by the echo command?Quote:
[justdemon ~]$ t="1\n"; t="$(echo -e $t)"; echo "$t"
1
[justdemon ~]$
Thanks.
command you are looking for might somthing look like this:-
If you still have a problem with this then provide us proper information what you exactly want to do? if possible provide example for the same.Code:[justdemon ~]$ t="1\n"; t="$(echo $t)"; echo -e "$t"
If t="1"$'\n', how to copy its content to other variables?
Here is the example without echo command.
Thanks.Code:[justdemon ~]$ t="1
> "; s="$t"; echo "$s"
1
[justdemon ~]$