Shell three execution modes (simple parameter description)

Source: Internet
Author: User

ShellThree ways to do it (not original, forget who's blog) 1: . Filename

1. SOURCE command usage: source FileName
Function: Reads and executes the commands in the filename under the current bash environment. The filename file can have no "execute permission"
Note: This command usually uses the command "." To replace.
such as: Source Bash_profile. Bash_profile are both equivalent.
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.
Point command, which is a dot symbol (from Bourne shell). is a sequential execution of the command in the file.

2:SHFilename

SH and Bash command usage: SH filename bash filename
Role: Create a new child shell in the current appropriate bash environment to read and execute the commands in the filename. The filename file can have no "execute permission"
Note: The difference between the two in executing the file is to use their own shell to run the files separately. SH uses the "-N" option to check the syntax of the shell script and uses the "-X" option to track the shell script-by-statement, using the shell's built-in variables to enhance the output information of the "-X" option.

 

3: ./Filename

./Command usage:  ./filename 
    role: Open a child shell to read and execute the command in the FileName.  
    The file must have executable permissions.  
   Note: When you run a shell script, another command interpreter is started.  
   each shell script runs efficiently in the parent shell A subprocess of .  this parent shell refers to a process that gives you a command designator in a control terminal or in a xterm window. The shell script can also launch his own subprocess. These child shells (that is, child processes) make the script parallel to the Efficiently run multiple subtasks within a script at the same time. The variable settings in the script are not valid in the parent process. The export in the script only works on the script and its sub-script positional variable parameters you can use this positional variable to get the arguments when passing parameters to the script. They were:  
   $0: script name. This variable contains an address that can be used basename

   to get the script name.  
   $1: first argument  
   $2,$3,$4,$5,... One analogy.

     Some script run related control information  
   $# the number of arguments passed to the script  
   $* Displays all parameters passed to the script in a single string  
   $$ The ID number of the script run  
   $! the ID number of the last process running in the background  
   [email protected] and $ #相同, but with quotation marks when used, and returns each parameter in quotation marks.  
   $-shows the current options used by the shell.  
   $? Displays the launch status of the last command. 0 indicates no error.

What is the difference between the source filename and the sh filename and./filename execution script?
1. When the shell script has executable permissions, there is no difference between using SH filename and./filename to execute the script:/filename because the current directory is not in path, all "." is used to represent the current directory.
2.sh filename re-establishes a child shell, executes the statement inside the script in the child shell, which inherits the environment variables of the parent shell, but the new, changed variables of the child shell are not brought back to the parent shell unless export is used.
3.source FileName: This command actually simply reads the statements inside the script and executes them sequentially in the current shell, without creating a new child shell. Then all the new and changed statements in the script will be stored in the current shell.


To illustrate:
1. Create a test. sh script with the content: A=1
2. Then make it executable chmod +x test.sh
3. After running SH test.sh, the Echo $A appears empty because A=1 is not passed back to the current shell
4. Run. After/test.sh, it is the same effect
5. Run source test.sh or. Test.sh, and then Echo $A, it will show 1, indicating that the a=1 variable is in the current shell?

Shell three execution modes (simple parameter description)

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.