The use of the CD command in the shell script

Source: Internet
Author: User

When writing the shell script to find that the CD switch directory can not switch, the code is the following.

1 #!/bin/bash2#changedir. SH3history4 cd/home/  Firefox516 pwd

I think carefully, I carry out the time is. Changedir.sh to execute, so that the terminal will produce a child shell, the child shell to execute my script, in the child shell has switched the directory, but once the child shell execution, immediately exit, the child shell variables and operations are all retracted. Back to the terminal, there is no change in the process at all.
So how do you verify the above conjecture? Look at the code below.

1 #!/bin/bash 2 #changedir. Sh 3  History 4 Cd/home/firefox 5 Sleep 1 6 pwd

First, according to the./changedir.sh execution, this time the terminal does not switch directories, the result of history execution is empty, stating that there is no historical command in the child shell (this is certain).
Then follow the source changedir.sh execution, this time is directly in the terminal shell execution script, did not generate a child shell, the result is the output history command, and switch directories.

The use of the CD command in the 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.