Talk about export and Source,bash in shell scripts.

Source: Internet
Author: User

Little brother just contact Linux, a lot of things on Linux is more unfamiliar, so write a blog, as their work summary and technology accumulation bar, but also spur their efforts to learn.

The reason for today's title is to put Export,source, Bash's three commands together, because they did a stupid thing. What kind of folly does not say first. Analyze these three commands first.

The first is the Export command.

    • 1.export command

The Linux Export command is used to set or display environment variables.

When executing a program in the shell, the shell provides a set of environment variables. Export can add, modify, or delete environment variables for use by subsequent executing programs. At the same time, it is important that the effect of export only extends to that landing operation. The environment variables given by the Export command do not exist, either by logging off or by opening a window again.

The syntax for this command is as follows:

Export [-fnp][variable name]=[variable setting value]

which

    • -F represents the function name in [variable name].
    • -n Deletes the specified variable. The variables are not actually deleted, but are not exported to the execution environment of the subsequent directives.
    • -p lists all the environment variables that the shell assigns to the program.

You can view all current environment variables by typing export only. If you want to add some new paths (such as/bin/bash) to an environment variable (such as path), you can use the following command format

Export Path=/bin/bash: $PATH

It's OK.

    • 2.bash command

Like the sh,bash,./command is used to execute shell scripts, the use cases are as follows:

[[email protected] bin] #bash  filename
[[email protected] bin] #sh filename
[Email protected] Bin]#./filename

These three forms are used to open a child shell to read and execute the command in the filename. Another command interpreter is started when you run a shell script. Each shell script effectively runs in a child process of the parent shell. This parent shell refers to the process of giving you a command designator in a control terminal or in a xterm window. The shell script can also start his own child process. These child shells, or sub-processes, enable the script to run multiple subtasks within the script in parallel and efficiently. Slightly different: Under the bash/sh command, the filename file can have no "execute permission", that is, there is no X permission in the rwx. For the./command, the filename file must have Execute permissions.

Next is the source command

    • 3.source command

The use case for this command is as follows:

[[email protected] bin] #source filename[[email protected] bin]#. FileName

Function: Reads and executes the commands in the filename under the current bash environment. The filename file can have no execute permission. This command usually uses the command "." To replace.

the source (or point) command is typically used to re-execute a newly modified initialization document. The source command (from C Shell) is a built-in command of the bash shell. The point command is a dot symbol (from the Bourne shell). The program body of source is bash, and the value of the $ variable in the script is bash. OK, now it's time to say what a stupid thing you've done. Usually before the execution of the script, you must first set up the environment variables you want to use, and then run their own program scripts. One morning I ran through the script and found out! Found! Found! /bin directory of the files are all deleted by me!!! At that time the heart was broken. So investigate the reasons. Only to find out the problem is on the Bash command and the source command. No nonsense,. Stupid I used the Bash command to set the environment variable (env.sh), after running the env.sh program back to the parent shell, the variable is not natural, and then run the work script, the result of the work script has RM statement, the result is tragic. So running programs under Linux is not so brain-free, and sometimes it's really a lot to think about. Get here today, come back later! (I hope not to delete the/bin directory of such a moth =)

Talk about export and Source,bash in shell scripts.

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.