0

Good day how to remove all the variables in the last slash in bash.

#!/bin/bash
VAR="/foo/bar/abcd ef gh"

I need to remove: abcd ef gh

path /foo/bar/ is always different

Seeking short notation ;-) thank you..

1 Answer 1

4

If your $VAR is always a path to a dir/file, you can use this:

$ VAR=$(dirname "$VAR")
$ echo $VAR
/foo/bar

Update: You can also use parameter substitution in bash:

$ echo ${VAR%/*}
/foo/bar
Sign up to request clarification or add additional context in comments.

1 Comment

Worth nothing the results are slightly different between dirname and parameter replacement in the case of values like /foo or foo - dirname turns them into / and ., respectively, while parameter replacement empties the first and leaves the second alone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.