Linux--#!/bin/bash

Source: Internet
Author: User
Tags posix

#!/bin/bash refers to the script that uses/bin/bash to interpret execution. Where #! is a special representation, followed by the shell path that explains the script. Bash is just one of the shells, and there are many other shells, such as: Sh,csh,ksh,tcsh,... We can experiment with one of the following examples to understand the use of #!/bin/bash. 1) #!/bin/bash can only be placed in the first line, if there are #!, then can only be regarded as a comment. There are three scripts (scripts will use the "chmod +x  scriptname" command to get executable permissions): Tbash1.sh:#!/bin/shsource abcecho "Hello abc"  tbash2.sh:# !/bin/bashsource Abcecho "Hello abc"  tbash3.sh:source abcecho "Hello abc"   Three script execution results: [[email protected ] other]$./tbash1.sh ./tbash1.sh:line 2:abc:no Such file or directory Note: When the source command performs a problem, SH no longer executes below. [[email protected] other]$./tbash2.sh ./tbash2.sh:line 2:abc:no such file or Directoryhello ABC Note: When the source command If there is a problem with the line, Bash continues with the following command. [[email protected] other]$./tbash3.sh ./tbash3.sh:line 1:abc:no such file or Directoryhello ABC Note: The s where the system itself is logged in Hell is bash. So, when the source command executes a problem, Bash continues to execute the following command.   If tbash1.sh is changed to: Echo "abc" #!/bin/shsource Abcecho "Hello abc" then the execution result is: [[email protected] other]$. Tbash1.sh abc./tbash1.sh:line 3:abc:no such file or DIrectoryhello ABC in other words, the script ignores the second line of "#!/bin/sh" and interprets the script directly using the current shell (that is, bash).   When the tbash1.sh is changed to: #!/bin/sh#!/bin/bashsource abcecho "Hello abc" execution result is: [[email protected] other]$./tbash1.sh  ./tbash1.sh:line 3:abc:no Such file or directory is not executed when the source command is finished. Note that the #!/bin/sh line worked, but #!/bin/bash did not work. In the script, not the first line of #!/bin/bash, just a comment.  2) #! The path behind must be correct, incorrect error will be. If we add a non-existent path to the #! of the first line in tbash1.sh "/home/sh": #!/home/shsource abcecho "Hello abc" the result is: [[email protected] other]$./tbash1.sh -bash:./tbash1.sh:/home/sh:bad interpreter:no such file or directory system will prompt that the path to/home/sh does not exist.  3) If a script does not add the #!+shell path to the first line, then the script will default to the shell that the current user is logged into as the script interpreter. In 1), the execution result of script tbash3.sh is the result of a shell (bash) that is currently logged in with itself. The shell we usually use is bash, and if you log in to SH one day and then use the above type of script, there will be a problem. The following is the result of logging on to SH and performing tbash3.sh:-sh-3.2$./tbash3.sh ./tbash3.sh:line 1:abc: No file or directory is executed with 1). Therefore, you should develop the habit of adding #!+shell paths to the first line of the script.  4)/bin/sh equivalent to/bin/bash--posix we changed the script tbash1.sh to: #!/bin/bash--posixsource abcecho "Hello abc" execution result:[[email  prOtected] other]$./tbash1.sh ./tbash1.sh:line 2:abc:no Such file or directory is the same as the result of tbash1.sh original script execution.   We can also use tbash3.sh as an example. Execute the script with the following command: [[email protected] other]$ bash tbash3.shtbash3.sh:line 1:abc:no such file or Directoryhello Abc[[emai L protected] other]$ sh tbash3.sh tbash3.sh:line 1:abc:no such file or directory[[email protected] Other ]$ Bash--posix tbash3.sh tbash3.sh:line 1:abc:no such file or directory  "bash tbash3.sh" means using BAS

Linux--#!/bin/bash

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.