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