1

I am SSH'd into an embedded board (Jetson TK1 running Ubuntu 14.04). I use the -X option so I can use gedit to edit text files. When executed from a terminal, gedit spews a bunch of garbage on the terminal. This is easy to get around by typing

gedit [file] > /dev/null 2>&1 &

with the last ampersand sending the process to the background so I can keep using the terminal. That gets tiresome to type out everytime so I want to define an alias:

alias gedit="gedit $@ > /dev/null 2>&1 &"

however this doesn't work. After the alias is defined (and for the initial try I just defined it directly on the bash command line), when typing

gedit [file]

Bash responds that

-bash: [file]: command not found

and opens gedit with with an untitled text file. Removing the trailing ampersand in the alias definition makes the alias open the specified file as expected, but now obviously doesn't send gedit to the background. What's wrong?

  • Have you already tried to close and then reopen terminal (this is needed to load the new alias)? Could also add to the question where did you define the new alias? – Danibix Apr 28 '16 at 14:58
  • I edited the question to add those details. – schrödinbug Apr 28 '16 at 16:18
0

My answer is heavily based on this post: http://ubuntuforums.org/showthread.php?t=2011495

An alias does not work like that, arguments are always passed to the end of the command. Using your alias as an example, gedit filename will actually run the following:

gedit $@ > /dev/null 2>&1 & filename

instead of

gedit filename > /dev/null 2>&1 &

Which is causing bash to think you inputting a second command called filename.

What you need to do instead, is create a function in your ~/.bashrc file, which will run the gedit command:

The following snippet is a slightly modified version of drmrgd's code

function gedit-bg() {
    if [ ! -n "$1" ]; then
        echo "USAGE: gedit-bg <filename>";
    else
        ( gedit "$1" > /dev/null 2>&1 & );
    fi
}

You will need to re-log for the change to take effect, once you do, you can use the method like so:

gedit-bg filename
0

The warnings from gedit on the server that I use for student accounts are very distracting to the students. Modifying Dan's answer a bit allows student to still type gedit as usual. I put this in /etc/profile.d/custom.sh (in Centos 7, but presumably it will work in Ubuntu also, or in a user's .profile file)

function gedit-bg() {
    if [ ! -n "$1" ]; then
        echo "USAGE: gedit <filename>";
    else
        ( \gedit "$1" > /dev/null 2>&1 & );
    fi
}

alias gedit="gedit-bg"
  • 1
    You don't need that alias. Just call the function gedit instead of gedit-bg, and use command gedit instead of \gedit in the function body. – muru Jun 6 '16 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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