How does shell change the current working path-original

Source: Internet
Author: User
Shell script execution has three methods: bash Script Name sh Script Name chmod + x Script Name test #. sh (extension not allowed) cdpwdechocomplete. However, during the execution, the path changed during the process. However, the final result is that the path is not changed. [fedora @ localhost ~] $ Basha. shcomplete [fed

There are three methods for executing shell scripts.
Bash Script Name sh Script NameChmod+ X Script Name
Use the following statement to test
# A. sh (the extension is optional)
Cd/
Pwd
Echo "complete"
However, during actual execution, the path is changed, but the final result is that the path is not changed.
[FEdOra @ localhost ~] $ Bash a. sh
/
Complete
[Fedora @ localhost ~] $
Cause analysis:
When the script is executed, only a sub-process is opened under the current shell. The directory switch operation is only valid for subsequent commands in the process, but cannot change the directory of the parent process.
Solution:
Method 1: Use source a. sh.
Method 2:
[Fedora @ localhost ~] $ Cd ~
[Fedora @ localhost ~] $ Chmod u + x a. sh
[Fedora @ localhost ~] $ ../A. sh
/
Complete
[Fedora @ localhost/] $
For $ ../a. sh
The first point is the bash internalCommandTo run in the current shell.
"./A. sh" is the command parameter, that is, the script to be executed.
Note: There must be spaces between two points.
 

A small Script: oop directory or file. If it is a directory, enter the directory. If it is a file, edit and change the file:
1. oop. sh Code #/bin/sh
# OpenFileOr directory
$1> &/dev/null
If [-d $1]; then
Cd $1;
ELsE
Vim $1;
Fi
2. the alias oop is source/tmp/oop. sh:
AliasOop = 'source/tmp/oop. Sh'

After executing oop commands in the future, folders and files will be free of will.

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.