Bash string replacing , find parent path

I wanted to do a string replacing with bash , which will find the parent path from the current running program's path.

So i tried to removing everything , from the last '/' to the end with regular expression:

echo ${0#/.*#}

But it's not working , and please don't let me use external programs , it's better getting everything with bash...

Thanks !


Do you mean something like this -

[jaypal:~/Temp] a="/Volumes/Data/jaypalsingh/Temp/file.txt"
[jaypal:~/Temp] echo ${a%/*}

So i tried to removing everything , from the last '/' to the end with regular expression:

> echo ${0#/.*#}

That is not a regular expression. Besides, you don't want the hash character (which removes a prefix), but a % sign (which removes suffix). In either case, that second hash character is not meant to be there (This is /not/ a regular expression. See the PARAMETER EXPANSION chapter in your bash manual).

You could use

echo ${a%/*}

to print the directory (but this breaks if "$a" does not contain a slash), or

echo ${a##*/}

to print the file (note the double ## to remove the longest matching prefix) (but this would break if "$a" was itself a directory and you wanted only that last component).

It's better to use the existing commands

dirname "$a"


basename "$a"

, respectively

Need Your Help

Lazarus: How do I find detailed docs (class info) of objects?

class object properties documentation lazarus

Is there a way to find complete class info of an object in Lazarus. F1 doesn't work.

How to index all chars in every item/key for a list/dictionary as unique ids

python list python-2.7 dictionary indexing

I would like to know if there is a more efficient way to reference every index of every item/key in a list/dictionary. Here's a bigger sample dictionary.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.