Results 1 to 8 of 8

Thread: bash alias in the background

  1. #1
    Join Date
    Oct 2011
    Beans
    41

    bash alias in the background

    Apologies if the following question is already answered here. With several combinations of google search I couldnt find any answer.

    How does one define an alias so as to run the command in background.
    e.g. I would like to define an alias
    $ > alias e='emacs'

    But would like to put it in the background, which is similar to
    $ > e a.txt &

    But rather than giving the ampersand `&` every time, is it possible to incorporate it in alias command?

    Thanks in advance

    VInay
    P.S.
    I am using Ubuntu 12.04 with bash (4.2.24(1)-release).

  2. #2
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    7,699

    Re: bash alias in the background

    I don't have emacs so I experimented with xeyes.
    alias e='xeyes &'
    seems to do the trick for me.

  3. #3
    Join Date
    Apr 2012
    Beans
    7,256

    Re: bash alias in the background

    I think the issue is the passing of the filename argument, not the backgrounding per se - you could do it with a simple bash function rather than an alias I think,

    Code:
    function e() { emacs $1 & }
    I don't know if this is good practice though... ymmv
    Last edited by steeldriver; June 27th, 2012 at 02:34 PM.

  4. #4
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: bash alias in the background

    I had that same thought - the issue was passing an argument - and you're right, I think you need a function to do it. I scribbled up the same thing, but with an error check (not that it matters...just out of boredom!):

    Code:
    function e() {
        if [ ! -n "$1" ]; then
            echo "USAGE: e <filename>";
        else
            ( emacs "$1" & );
        fi
    }

  5. #5
    Join Date
    Oct 2011
    Beans
    41

    Re: bash alias in the background

    Thanks steeldriver,
    The trick seem to work, irrespective of it is a common practice or not!

    @Experts: is there any drawback of this trick?

    Thanks once again

    VInay

  6. #6
    Join Date
    Apr 2012
    Beans
    7,256

    Re: bash alias in the background

    you should definitely use drmrgd's version not mine - as well as error checking he remembered to quote the argument! (in case the filename has spaces etc.)

  7. #7
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: bash alias in the background

    +1 to drmrgd's example


    aliases work like simple text substitution and they act as a single block, it's not possible to split them into functional parts. Your arguments are always at the end.
    alias a='cmd -a b -c d e ...' when used
    $ a p1 p2
    becomes
    $ cmd -a b -c d e ... p1 p2


    in cases where you need something fancier (like here, with something specific at the very end), you want functions or scripts. With them you have the total freedom how you want to construct the actual command to be executed.

  8. #8
    Join Date
    Oct 2011
    Beans
    41

    Re: bash alias in the background

    Thanks all of you for the quick support. drmgd's reply is really fantastic. I also tried it with the filename with spaces.

    Thanks once again.

    -- VInay

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •