Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 20
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    The echo behavior seems incorrect with/without -e option.


    Could any one tell me why the below echo command seems to behave inversely? Thanks.

    [justdemon ~]$ t="1\n"; t=$(echo -e $t); echo -e $t
    1
    [justdemon ~]$ t="1\n"; t=$(echo $t); echo -e $t
    1

    [justdemon ~]$

  2. #2
    what do you mean by inversely??

    what answer are you expecting?

  3. #3
    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.

  4. $spacer_open
    $spacer_close
  5. #4
    Dear patelhemal2210,

    Thanks very much for your detailed explanation.
    It looks like I got incorrect idea of -e option.

    Best Regards,
    justdemon

  6. #5
    you are always welcome.

  7. #6
    Just Joined!
    Join Date
    Mar 2007
    Location
    Bogotá, Colombia
    Posts
    46
    Quote Originally Posted by justdemon View Post
    Could any one tell me why the below echo command seems to behave inversely? Thanks.
    Bash is doing what it's supposed to do.

    When you execute the first $(echo -e $t), bash is already interpreting the escape character, and so it assigns 1 to your variable.

    try this:

    Code:
    user@host$ A="
    > 
    > 1"; echo -e $A
    1
    user@host$

  8. #7
    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.
    [justdemon ~]$ t="1\n"; t="$(echo -e $t)"; echo "$t"
    1
    [justdemon ~]$
    How to do to assign a newline included in the original variable into a new variable by the echo command?

    Thanks.

  9. #8
    command you are looking for might somthing look like this:-
    Code:
    [justdemon ~]$ t="1\n"; t="$(echo $t)"; echo -e "$t"
    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.

  10. #9
    If t="1"$'\n', how to copy its content to other variables?

    Here is the example without echo command.
    Code:
    [justdemon ~]$ t="1
    > "; s="$t"; echo "$s"
    1
    
    [justdemon ~]$
    Thanks.

  11. #10

    Smile

    Quote Originally Posted by justdemon View Post
    Code:
    [justdemon ~]$ t="1
    > "; s="$t"; echo "$s"
    1
    
    [justdemon ~]$
    .
    Yeap.. this is working.

    you could copy the content of "t" into "s" variable.

    what else do you want?

    what exactly do you want do?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •