Delivery of data between commands in the shell

Source: Internet
Author: User

1. Piping "|"

ls | Cat-n > OUT.txt

2. Child shell

2.1 Child Shell Description

You can use the () operator to define a child shell in a shell script

#/bin// ; ls) pwd

Execution results

  

Description: Does not have any effect on the current shell when the command executes in a child shell

2.2 Getting data from a child shell

cmd_output=$ (COMMANDS)

out=$ (ls-l | cat-n)

#!/bin/bashout=$ (lscat -N)echo $out

Output Result:

  

The problem: the return key between rows and rows is gone. This is obviously the result we don't want.

  Workaround: Enclose the result of the child shell in double quotes, preserving line breaks

  

#!/bin/bashout=$ (lscat -N)echo  $outout="$ ( Ls-l | cat-n)"echo $out

Execution Result:

  

What the heck!!! There is no change. Is it wrong to write in the book?!

Finally found the answer to the question on the Internet: IFS (internal domain delimiter)

IFS Details: http://www.2cto.com/os/201504/394478.html

The IFS is set to "\ n" line breaks.

  

#!/bin/bashout=$ (lscat -N)echo  $outIFS="\ n  "out ="$ (ls-l | cat-n)"echo $out

  

3. Anti-citation (not a single quote)

out.txt= ls | Cat-n

Other operations are the same as child shells

Delivery of data between commands in the shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.