Script Demo: Github address

Switch between one or two directories: cd-cd – goes back to the previous directory,

“-” is equivalent to the $OLDPWD variable. The $OLDPWD variable is the previous directory recorded by bash, and either CD – or CD $OLDPWD can be used to cut between the two most recently operated directories.

Switch between multiple directories pusHD popd dirs

Pushd: switches to the directory as an argument and pushes the original and current directories into a virtual stack; If no parameter is specified, the system returns to the previous directory and swaps the two nearest directories in the stack. Popd: pops the nearest directory in the stack. Dirs: lists the directories saved in the current stack

1. The -p parameter displays a list of directories in the stack, one per line. The -v parameter can be preceded by a directory number. 2. If -v exists, one directory per line can be displayed without -p. 3

Ex. :

Jump to the script directory
pushd `dirname $0` > /dev/null
CD $(dirname "$0"); ' 'is equivalent to $()

working_path=`pwd`
popd > /dev/null

cd ${working_path}
Copy the code

The dirname command takes the directory portion of a given path. This command is typically used in shell scripts to get the directory where the script file is located and then switch the current directory there. Another command under Linux is basename, which, as opposed to dirname, gets the file name part.

“> /dev/null” : the standard output is redirected to an empty device file, that is, no information is output to the terminal, that is, no information is displayed.

> : Indicates where to redirect the output 123 to the 123.txt file, for example, echo “123” > /home/123. TXT

Popd removes the top directory from the stack and switches to the new top directory

Both pusHD and POPd can affect only the stack without switching directories. Use the -n argument

Dirs clears the directory stack with the -c argument

Perform:

“. “points to the current directory. If yourscript is in the current directory, you can use the following command:./yourscript.sh