dirname command under Linux

1. Introduction to the dirname command

The dirname command strips the non-directory part of the filename and only displays the content related to the directory. The dirname command reads the specified pathname keeping the last / and the following characters, removes the rest, and writes the result to standard output. If there are no characters after the last /, the dirname command uses the second-to-last / and ignores all characters after it. dirname and basename are usually used in shell internal command substitution to specify an output filename that differs slightly from the specified input filename.

Example one example from man page
$ dirname /usr/bin/sort
/usr/bin

Example two
$ dirname /usr/bin
/usr

Go to the directory where the current script is located

cd `dirname $0`
或者
cd $(dirname $0)

Enter the parent directory where the current script is located

cd `dirname $0`/..

Get the absolute path where the script file is located in the script

shellPath1=$(dirname $0)
或者
shellPath1=`dirname $0`
echo $shellPath1

Guess you like

Origin blog.csdn.net/qq_17576885/article/details/123275167