Shell script return, ECHO, printf

Source: Internet
Author: User

Ehco and return

return   Returned is the status code, function end Return does not return function return value You can return the function return value with echo in front, return the specified function exit status code

Echo returns the function return value, the function exit status code is the exit status code of the last command of the function

test1.sh

#!/bin/bash     function test () {     echo 200                                                                        }       ret=$ (test) echo  "####$?" echo  "*********** $ret" 

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/83/0F/wKiom1dp_6HiScuQAAAL1HLIpVY817.png "title=" 1.PNG " alt= "Wkiom1dp_6hiscuqaaal1hlipvy817.png"/>

T est2.sh

#!/bin/bash     function test () {     return 200                                                                     }        ret=$ (test) echo  "####$?" echo  "*********** $ret" 

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/83/0E/wKioL1dp_7Sx8mF7AAAMGcFT51M234.png "title=" 2.PNG " alt= "Wkiol1dp_7sx8mf7aaamgcft51m234.png"/>


In the case of bash, the return command can only be used in a function, not directly in the script

The built-in command return is used for returning from the function, ..., the return command is not required in the function, and when the function is called, the command inside the function is returned to the place where it was called (typically, it is returned to the main program). If a function has a return command, it returns when it executes to return. If return is not the last command in the function, then the other commands after return are no longer executed.



Ehco and printf

Common commands for terminal printing are two types of echo and printf


1, echo Hello Word!

This method cannot print a semicolon because the semicolon is used as a command qualifier in the Shell to mark the end of a command.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/83/10/wKiom1dqA4ehVmTUAAAkVH-1pKk415.png "title=" 3.PNG " alt= "Wkiom1dqa4ehvmtuaaakvh-1pkk415.png"/>


2. Echo ' Hello Word! '

Single quotation marks are printed in a way that does not print variables.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/83/0F/wKioL1dqA5qzJh5VAAArxHLXbuc005.png "title=" 4.PNG " alt= "Wkiol1dqa5qzjh5vaaarxhlxbuc005.png"/>

3. Echo "Hello World"

The exclamation mark cannot be printed directly in double quotation marks, and set +h must be added.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/83/0F/wKioL1dqBELyMIvBAAAbEbl3_4U829.png "title=" 8.PNG " alt= "Wkiol1dqbelymivbaaabebl3_4u829.png"/>


Echo's Parameters

-E: Identify escape sequences in output content

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/83/10/wKiom1dqBF6xT6pRAAAg3waBNDY761.png "title=" 5.PNG " alt= "Wkiom1dqbf6xt6praaag3wabndy761.png"/>

-N: Ignore trailing line breaks

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/83/0F/wKioL1dqBGuC7UODAAAmYnRwlsM762.png "title=" 6.PNG " alt= "Wkiol1dqbguc7uodaaamynrwlsm762.png"/>


For printf, the two points to be stated are

The default output for 1.printf does not wrap, and the line break needs to add "\ n" Yourself

2.PRINTF supports formatted output



Finish

This article from "Zero Egg" blog, declined reprint!

Shell script return, ECHO, printf

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.