Shell's recommendations

Source: Internet
Author: User
Tags shellcheck
Specify bash

Shell script first line, #! What should it be?

Most of the cases/usr/bin/env bash,/bin/bash,/usr/bin/bash,/bin/sh,/usr/bin/env sh are equivalent, but few cases still have pits. If the default shell may not be bash, such as a release law, the default SH is not bash.

So we recommend using/bin/bash

Set-e and Set-x

Insert a line of SET-E and a line before the specific code logic set-x

Set-x will output the execution when executing each line of the shell script. All variables involved will be replaced with actual values.

SET-E will end the program in the execution of an error. (but not all errors will end the program).

Bring Shellcheck.

Shellcheck can check the wrong

Note Local

When writing a function, if you do not add the local qualifier, then the default is the global variable, which is similar to JS.

In a top-level scope, there is no global impact, but in a function, a global variable can contaminate other scopes. So the variables declared in the function must be prefixed with the local qualifier.

Complex logic or avoiding the shell

Shell scripting is often difficult to transplant, it is difficult to unify the error processing, difficult to process data. So you need to work with a lot of external commands, such as Grep,sed,awk. So solving a particular problem requires a specific tool, switching to a more generic scripting language, sometimes better.

Shell's recommendations

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.