How the shell changes the current working path

Source: Internet
Author: User

Reproduced:

http://imysqldba.blog.51cto.com/1222376/616805

There are three ways to execute a shell script
Bash script name sh script name chmod +x
Use the following statement to test
#a. SH (does not have extension)
CD/
Pwd
echo "Complete"
However, the actual execution time, the discovery process changes the path, but the end result is not changed the path
[Email protected] ~]$ bash a.sh
/
Complete
[Email protected] ~]$
Cause Analysis:
When executing a script, only a child process is opened under the current shell, and the operation of the switch directory is only valid for the relevant subsequent instruction in the process, but the directory of the parent process cannot be changed.
Workaround:
Law one: a.sh with source.
Law II:
[Email protected] ~]$ CD ~
[Email protected] ~]$ chmod u+x a.sh
[Email protected] ~]$. ./a.sh
/
Complete
[Email protected]/]$
For $. ./a.sh
The first point is the internal command of bash, which means running in the current shell
The following "./a.sh" is the argument to the command, which is the script to execute
Note: There should be a space between two points

A small script: Oop directory or file, if it is a directory, then enter the directory, if it is a file, then edit the file:
1. oop.sh Code #/bin/sh
# Open a file or directory
$ >&/dev/null
If [-D $];then
CD $;
Else
Vim $;
Fi
2. The alias oop is source/tmp/oop.sh:
Alias oop= ' source/tmp/oop.sh '

After you execute the OOP command, folders, files are arbitrary

How the shell changes the current working path

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.