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.

openup /Users/de/Downloads/32/S01E32-array-arrayslice-collection-collections-1-master.zip

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ᴇᴌᴀ, Graipher Feb 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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.