Introduction to the Linux shell--bash Shell script

Source: Internet
Author: User
Tags echo command

Bash shell Script Introduction Shell Runtime Environment

If you're running a Unix or Linux system, such as Ubuntu,red Hat,suse Linux and MacOS, it's built-in bash shell, so you don't need to configure the so-called development environment.

My shell environment is the MacOS Sierra version, and if you're using a different Linux system, the following examples are basically operational.

First, open the terminal command line and check the shell version of your system first:

Echo $BASH _version

Bash Command hierarchy command type

The bash Shell has a built-in type command that displays the type of this command based on the words you enter, mainly in the following five types:

    • Alias

    • Method

    • Shell built-in commands

    • Key words

    • File

For example, we often use cd commands that let us perform the following command to see what type it belongs to.

Type cd

Also, in order to view more detailed information, you can use the

Type-a CD

If the information you want to see is more concise and appropriate for people to understand, you can use the following commands and parameters:

Type-t ls

PATH command

Linux checks whether a program that is configured to specify a path in the path environment can execute. Normally, the current directory is not looked up, and unless you configure it to path, we can execute the following command to add the current directory to the PATH environment.

Export path= $PATH:.

Next, we create a directory bin that holds shell scripts, which can be executed with the following command:

$ test-d $HOME/bin | | mkdir $HOME/bin

Of course you can create the directory bin manually in your home directory. The above meaning is to check whether the home directory has a bin directory, not created.

Create a script

Nothing to say, the first program to learn every language is "Hello,world", the file is called hello1.sh.

#! /bin/bashecho "Hello World" Exit 0

Explain:
#!/bin/bash: Typically, the default first line of code for a script is it. “#!” and became shebang. It is used to tell the system's interpreter to execute the script. In addition to bash, we can also php,perl and other scripts.
echo "Hello World": Echo is a built-in command to represent standard output, similar to the one in JavaSystem.out.println()
exit 0: Indicates that the script ends the exit,exitThere is an integer parameter, 0 indicates a normal exit, and a non 0 indicates an error in the execution of the script.

Execute script

Now, let's execute the script above, you can go to the directory where the script file exists at the command line, or it can be in any directory, but the path to the file should be an absolute path at execution time:

Bash $HOME/bin/hello1.sh

The result is, "Hello World" is printed out.

Tip
In the execution of the above command, you may get an error, prompt for insufficient permissions, or access denied errors. This is because hello1.sh does not have permission to execute. So we use the following command to add the corresponding permissions to the file.
chmod +x $HOME/bin/hello1.sh

Some special parameters in the script

In the script, some of the parameters that represent special meanings are listed below in several common:

parameter Identifiers meaning
$ The name of the file itself
$ A parameter that represents a position, the first argument passed to the script
${10} Use curly braces to limit the number of arguments that exceed two digits
$# Number of parameters
$* Represents all the parameters

As shown below:

#! /bin/bashecho "file name $ (basename $)" echo "Hello" echo "Hello $*" echo "Args Count: $#" Exit 0

The result of the output is:

Pay attention to the correct use of quotation marks

By now, we have used double quotes to enclose the string for the output of the echo command.
In the first hello1.sh, the effect is the same with single or double quotes. The following two lines of code are equivalent.

echo ' Hello World ', echo ' Hello World '

However, when you include a reference with a variable, the effect of single and double quotes is different.

echo "Hello $"//print the passed value, for example Tim. Echo ' Hello $ '//print out the original

Therefore, in a variable string, it is recommended to use double quotation marks. At this point, $ $ will be replaced by the value of the variable instead of being displayed as a string.

Print Script Name

As mentioned earlier, special parameters are $0 used to denote the name of the script, where the name will have the full path, if we only want the file name, you can use the following code:

echo "You are using $ (basename $)"

Here $ (...) the function of the syntax is that we first execute the command inside the parentheses, and then assign the result to an unknown variable outside.
$ (...) syntax there is also an equal notation, note that the keys on the left of the number 1 on the keyboard are not single quotes.

echo "You are using ' basename $ '"

Individuals do not recommend this type of writing, too easy to confuse, not easy to mistake.

To debug your script

If you want to debug your script, bash gives us two options:-V and-X.
You can use the-v option if we want to see the contents of the script in detail in a row.

#! /bin/bashecho "file name $ (basename $)" echo "Hello" echo "Hello $*" echo "Args Count: $#" Exit 0

More commonly, the-X option, which displays commands when they are executed. When we decide to choose a branch, we use it more.


As you can see, the basename first execution, using this option will not see the code details.


Introduction to the Linux shell--bash Shell script

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.