You are not logged in.

#1 2010-11-10 01:09:11

Halcyon22
Member
Registered: 2010-07-30
Posts: 33

[SOLVED] Simple .bashrc function problem

Hi, I'm trying to set up a function in my .bashrc to automatically direct stdout and sterr to /dev/null as well as background the process. The only problem I'm having is trying to set it up so I can use it with a program and its arguments, like "gedit file.txt". So far I've tried

q() {
    "$@" &> /dev/null &
}
q() {
    "$*" &> /dev/null &
}
q() {
    "$1 $2" &> /dev/null &
}

and without quotes. Every time it returns exit status of 127, "command not found" probably because it tries to run file.txt, it does show a blank gedit window.
I want to be able to type "q gedit file.txt" instead of having to type "gedit file.txt &> /dev/null &" every time. Thanks for the help.

Last edited by Halcyon22 (2010-11-10 10:25:43)

Offline

#2 2010-11-10 04:08:00

Halcyon22
Member
Registered: 2010-07-30
Posts: 33

Re: [SOLVED] Simple .bashrc function problem

I seem to have fixed it with.

q() {
    arg[1]=$1
    arg[2]=$2
    ${arg[@]:1:2} &> /dev/null &
}

Offline

#3 2010-11-10 06:01:53

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [SOLVED] Simple .bashrc function problem

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

works for me. (q gedit foo.txt)

man bash wrote:

"$@" is equivalent to  "$1"  "$2" ...

Edit:
BTW, in your last post, ${arg[@]:1:2} will not work if "$2" contains spaces. You should put double quotes around it. Or, you could have just written:

q() { "${@:1:2}" &>/dev/null & }

which is equiv. to

q() { "$1" "$2" &>/dev/null & }

which, of course, is less "portable" than "$@" wink

Last edited by lolilolicon (2010-11-10 10:45:02)


This silver ladybug at line 28...

Offline

#4 2010-11-10 10:24:50

Halcyon22
Member
Registered: 2010-07-30
Posts: 33

Re: [SOLVED] Simple .bashrc function problem

Hmmm you're right, thanks for clearing it up. smile

Offline

Board footer

Powered by FluxBB