Find the answer to your Linux question:
Page 2 of 2 FirstFirst 1 2
Results 11 to 20 of 20
I am wondering why I can't use echo command to do the same thing. In pipeline, I could echo the variable including the newline. But I can't assign a variable ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11
    Just Joined!
    Join Date
    Oct 2012
    Posts
    9

    I am wondering why I can't use echo command to do the same thing.

    In pipeline, I could echo the variable including the newline.
    But I can't assign a variable as the result of echo command.

    What I can do is to "copy" them directly.

    I can't understanding why there is a such weird limitation in scripting.

    The real case is that I can't use the echo command as a "Pattern" or a "Macro" if I need to auto-generate a script.
    Sorry, but I can't explain it easily by simple words.

    Thanks.

  2. #12
    Just Joined!
    Join Date
    Aug 2012
    Posts
    28
    You can do it with echo but you are not understanding the use of -e flag.

    for example,

    s=$t & s=$(echo $t) both will assign the same value to "s".

    Does that make it clear to you anything?
    or still blank?

    what you want do actually?
    have you written any script and not getting the expected result from script???
    then just provide us that script and tell us expected output.


    we might help you out.

  3. #13
    Just Joined!
    Join Date
    Oct 2012
    Posts
    9
    From my result, s=$t & s=$(echo $t) are different.
    Code:
    [justdemon ~]$ t="1"$'\n'; s=$t; echo "$s"
    1
    
    [justdemon ~]$ t="1"$'\n'; s=$(echo $t); echo "$s"
    1
    [justdemon ~]$
    I don't know how to use the echo command to reserve the "newline" charater.

    Thanks.

  4. #14
    Just Joined!
    Join Date
    Aug 2012
    Posts
    28
    why are you assigning "t" such an odd value you can do as bellow,

    Code:
    ph2210@ubuntu-022:~$ t="1\n";s=$(echo $t);echo $s
    1\n
    ph2210@ubuntu-022:~$ t="1\n";s=$(echo $t);echo -e $s
    1
    
    ph2210@ubuntu-022:~$ t='1\n';s=$(echo $t);echo $s
    1\n
    ph2210@ubuntu-022:~$ t='1\n';s=$(echo $t);echo -e $s
    1
    
    ph2210@ubuntu-022:~$
    here if you use -e flag then it will not display \n instead it will put "enter".
    and if you do not use -e flag then it will display \n.

    Hope this would help you.

  5. #15
    Just Joined!
    Join Date
    Aug 2012
    Posts
    28
    Quote Originally Posted by justdemon View Post
    Code:
    [justdemon ~]$ t="1"$'\n'; s=$t; echo "$s"
    1
    
    [justdemon ~]$ t="1"$'\n'; s=$(echo $t); echo "$s"
    1
    [justdemon ~]$
    or you can use this ,
    Code:
    ph2210@ubuntu-022:~$ t="1"'\n'; s=$t; echo "$s"
    1\n
    ph2210@ubuntu-022:~$

  6. #16
    Just Joined!
    Join Date
    Oct 2012
    Posts
    9
    It is because $'\n' == "newline" which is not the same as "\n" or '\n'.

    Or I could change the example as the following, and I can't still find out a solution with echo command.

    Code:
    [justdemon ~]$ t="1
    > "; s=$t; echo "$s"
    1
    
    [justdemon ~]$ t="1
    > "; s=$(echo $t); echo "$s"
    1
    [justdemon ~]$

  7. #17
    Just Joined!
    Join Date
    Aug 2012
    Posts
    28
    OK.
    now I got you point.

    yeap .you are right echo does not reserve last new line character.
    but it accepts new line character in between.

    see the below code:-

    Code:
    ph2210@ubuntu-022:~$  t="1
    > 
    > 
    > 2";s=$(echo "$t");echo "$s"
    1
    
    
    2
    ph2210@ubuntu-022:~$

  8. #18
    Just Joined!
    Join Date
    Aug 2012
    Posts
    28
    it is sometime advantage of this echo behavior. check it out in this script:-
    Code:
    #!/bin/sh
    
    a='how
    are
    you
    justdemon
    '
    
    b=`echo ${a}`
    c="${a}"
    
    echo "b-> $b"
    echo "c-> $c"
    Run this script and check out the difference.

    Code:
    ph2210@ubuntu-022:~$  ./test.sh 
    b-> how are you justdaemon
    c-> how
    are
    you
    justdaemon
    
    ph2210@ubuntu-022:~$
    See the difference and its advantage.????
    its very cool.

  9. #19
    Just Joined!
    Join Date
    Oct 2012
    Posts
    9
    The difference could be made by quotes like the following example.
    But I don't understand why we can't reserve the last newline.
    Code:
    #!/bin/sh
    
    a='how
    are
    you
    justdemon
    '
    
    b=`echo ${a}`
    c="${a}"
    d=`echo "${a}"`
    
    echo "b-> $b"
    echo "c-> $c"
    echo "d-> $d"
    The result should be
    Code:
    ph2210@ubuntu-022:~$  ./test.sh 
    b-> how are you justdaemon
    c-> how
    are
    you
    justdaemon
    
    d-> how
    are
    you
    justdemon
    ph2210@ubuntu-022:~$

  10. #20
    Just Joined!
    Join Date
    Oct 2012
    Posts
    9
    I've found out the answer of the missing newline from the below link.
    hxxp://unix.stackexchange.com/questions/17747/why-does-shell-command-substitution-gobble-up-a-trailing-newline-char
    (Sorry, no permission to post a URL)

    In summary, it's a standard behavior in linux shell that the $() will remove sequences of one or more <newline>s at the end of the substitution.

Page 2 of 2 FirstFirst 1 2

Posting Permissions

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