shell - How to print the name of my current directory's parent directory -


i hope phrased question clearly. specifically, this:

i use test or perhaps case statement determine if directory name contained in variable $dir either current directory (.) or it's parent directory (..)

of course can echo current directory (echo $pwd), simplest way echo parent of $pwd?

thanks.

you can use ${pwd%/*}:

$ echo $pwd /home/me/test/t $ echo ${pwd%/*} /home/me/test 

as expression ${pwd%/*} stripping shortest match of /* of $pwd:

from bash reference manual → 3.5.3 shell parameter expansion:

${parameter%word}

the word expanded produce pattern in filename expansion. if pattern matches trailing portion of expanded value of parameter, result of expansion value of parameter shortest matching pattern deleted. if parameter ‘@’ or ‘’, pattern removal operation applied each positional parameter in turn, , expansion resultant list. if parameter array variable subscripted ‘@’ or ‘’, pattern removal operation applied each member of array in turn, , expansion resultant list.


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -