# Mac Bash: how to combine the open and str subtract to one line code? [closed]

new to bash , I use the following code to open the super folder with a file path.

function openUp(){
cd $(echo$1 | sed 's@$$.*$$/.*@\1@' );
open ..
}


like drag a file to terminal, then open the super folder conveniently.

How to combine the two line code to one line?

I originally think open directly without changing directory.

## closed as off-topic by Toby Speight, Austin Hastings, vnp, Sᴀᴍ Onᴇᴌᴀ, GraipherFeb 19 at 16:54

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Toby Speight, Austin Hastings, vnp, Sᴀᴍ Onᴇᴌᴀ, Graipher
If this question can be reworded to fit the rules in the help center, please edit the question.

You can use open with an absolute path, it doesn't have to be a relative path.

That is, you could write the same in one line, with some basic improvements:

openUp() {
open "$(sed 's@$$.*$$/.*@\1@' <<< "$1")/.."
}


The basic improvements:

• Instead of echo "..." | cmd, use here strings: cmd <<< "..."
• Double-quote variables used as command line arguments (in your example, of cd, and echo
• It's not recommended to use the function keyword, write without

A more important improvement would be to stop using sed to get the name of the base directory. Using a regex is error-prone and not as intuitive as the dirname command:

openUp() {
open "$(dirname "$1")/.."
}


Notice that the arguments of dirname and open are both double-quoted, as mentioned earlier. This is necessary, to protect from word-splitting and globbing.

• open "$(dirname "$1")/.." works perfect. while open "$(sed 's@$$.*$$/.*@\1@') <<< "$1")/.." get stuck there, just holding on. I tested in Mac. – black_pearl Feb 18 at 6:48
• open "$((sed 's@$$.*$$/.*@\1@') <<< "$1")/.."; , lack a close parenthesis. – black_pearl Feb 18 at 6:55
• @black_pearl oops, typo, fixed it, thanks! – janos Feb 18 at 6:57