4
\$\begingroup\$

I use terminal alot, so it was frustrating to me to deal with gedit's inconsistent behavior between having a window open and when one was not. I ended up searching around, and finding a way of getting around this. I've created this bash script, and aliased it to gedit, and I've slowly been expanding it.

Basically the script does these things:

  • Checks to see if arguments exist, if not, it opens gedit with no document
  • If arguments do exist, it iterates over each and opens them all.

This creates consistent behavior where gedit won't stick in terminal dumping log data.

#!/bin/bash

if [[ -z $1 ]]; then
    /usr/bin/gedit &
else
    for var in "$@"
    do
        /usr/bin/gedit "$var" &
    done
fi

My question is if there are any glaring errors here, or if there is a cleaner, faster way of doing this? I'm specifically interested in the loop, as I feel there should be an easier way of passing list data as a single string. The reason I didn't just use $* for that, is when I tried the fact that it's wrapped in quotes caused it to open a single document with all parameters listed as the file name.

I added the quotes for a reason that I should have documented, but I currently do not remember.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ You probably added the quotes to be able to handle space in the pathname. \$\endgroup\$
    – iveqy
    Commented Aug 18, 2014 at 15:25
  • \$\begingroup\$ I do believe you're right, as that does sound like an issue I would have had to overcome. I'm going to just assume you're right, since trying to test to see if passing a spaced file name using quotes would work incorrectly in this instance would be a pain (and I'm assuming it would) \$\endgroup\$ Commented Aug 18, 2014 at 15:29
  • \$\begingroup\$ The edit info may be a Community Wiki answer instead. It doesn't belong in the question itself. \$\endgroup\$
    – Jamal
    Commented Aug 19, 2014 at 15:06
  • \$\begingroup\$ How do I go about adding it there? \$\endgroup\$ Commented Aug 19, 2014 at 15:07
  • 1
    \$\begingroup\$ Post it as a new answer at the bottom of the page. \$\endgroup\$
    – Jamal
    Commented Aug 19, 2014 at 15:08

2 Answers 2

4
\$\begingroup\$

If you just want to make gedit operate silently and in the background, I would create a function (save in ~/.bashrc)

gedit() { command gedit "$@" &>/dev/null & }

Also, Accessing bash command line args $@ vs $*

\$\endgroup\$
2
  • \$\begingroup\$ This doesn't solve the issue of spacing. gedit test\ file.php test\ file2.php still opens three separate files. \$\endgroup\$ Commented Aug 19, 2014 at 14:59
  • \$\begingroup\$ My mistake, I had forgotten to quote $@ \$\endgroup\$ Commented Aug 19, 2014 at 15:00
0
\$\begingroup\$

I accepted the answer here due to the linked post. I had originally been using $* as I stated above, and was unaware of the differences between $* and $@. As it turns out, in this instance I needed $@ because $* passes the list of arguments as a single string with all special characters quoted, including any spacing between arguments, while $@ passes the arguments as a single string, but with only the arguments quoted, so spacing between arguments is treated as spacing for the command.

Given that, I have converted the above script over to the following:

#!/bin/bash

if [[ -z $1 ]]; then
        /usr/bin/gedit &
else
        /usr/bin/gedit "$@" &
fi
\$\endgroup\$
2
  • 2
    \$\begingroup\$ when you have no command line arguments, "$@" expands to nothing, so you don't really need the if statement \$\endgroup\$ Commented Aug 19, 2014 at 18:00
  • \$\begingroup\$ That's perfect for my goal of cutting down the code \$\endgroup\$ Commented Aug 19, 2014 at 18:21

Your Answer

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

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