0

I have an alias, that runs a function (named timer) with nohup. I like to pass a parameter to the alias, but it doesn't work.

Here is what I have in my bashrc:

timer() {
    arg1=$@
    echo "$arg1"
    # ...

}

export -f timer


alias timer_ugly='nohup bash -c timer "$@" &'

I also tried

alias timer_ugly='nohup bash -c timer "$1" &'

is there way to make my alias take an argument that get passed to my function ?

Update: I figured out alias does not take parameters, and I tried to make a function instead and that doesnt work either.

this also didn't work: the function takes the parameter but nohup doesnt.

timer_nohup() {
    echo "TODO "
    echo $@

    nohup bash -c timer $@ &
}
Medya Gh
  • 4,563
  • 5
  • 23
  • 35
  • 2
    You cannot make an alias that takes parameters. It is not possible in bash. You can make your function perform the tasks that you wanted to do within your alias, though. – jeremysprofile Jul 17 '18 at 19:33
  • 1
    That said, `nohup` is generally pointless -- bash has built-in capabilities that can do everything the external `nohup` tool can. See `help disown`. – Charles Duffy Jul 17 '18 at 19:34
  • try `alias timer_ugly='nohup bash -c "timer $@ &" -- '` – KamilCuk Jul 17 '18 at 19:35
  • `arg1=$@`, btw, is compressing your argument list to a string. That list is an array. You can't store an array of arbitrary strings inside a single string without losing data. – Charles Duffy Jul 17 '18 at 19:36
  • `timer() { args=( "$@" ); printf -v args_q '%q ' "${args[@]}"; echo "About to run: $args_q"; start_time=$SECONDS; "${args[@]}"; end_time=$SECONDS; echo "Took $(( end_time - start_time ))"; }` serializes your list of arguments in a way that doesn't lose details (f/e, making it impossible to distinguish between `timer "first argument" "second argument"` and `timer "first" "argument" "second" "argument"`). – Charles Duffy Jul 17 '18 at 19:38
  • ...anyhow, the issue with your alias is that it appends the arguments after the `&`. `&` is a command separator, so everything after it is considered a different, separate command. A function doesn't have that issue. – Charles Duffy Jul 17 '18 at 19:38
  • Your function is broken for different reasons -- it's not an inherently-broken approach, but the quoting is wrong. – Charles Duffy Jul 17 '18 at 19:39
  • @jeremysprofile I tried that and didn't work (updated the question) – Medya Gh Jul 17 '18 at 19:39
  • 1
    `timer_nohup() { nohup bash -c 'timer "$@"' _ "$@" & }` -- quotes are important; you can't just leave them out. `$@` outside quotes means something completely different than what it means inside them. – Charles Duffy Jul 17 '18 at 19:40
  • ...but again, using `nohup` is pointless in the first place, and you're better off not doing it. – Charles Duffy Jul 17 '18 at 19:41
  • `timer_nohup() { timer "$@" >nohup.out 2>&1 – Charles Duffy Jul 17 '18 at 19:42
  • Thank you so much @CharlesDuffy for such informative answer, solve my problem, but disown is even better, I opted out to use your suggestion disown. btw I wonder what doe s _ mean in your answer ```nohup bash -c 'timer "$@"' _ "$@" ``` ? – Medya Gh Jul 17 '18 at 19:51
  • The `_` is a placeholder for `$0`, such that `"$@"` expands into `"$1"` and onward. – Charles Duffy Jul 17 '18 at 19:56
  • sweet do you mind if I add your name to the contributors here ? @CharlesDuffy https://github.com/medyagh/bash_timer/blob/master/contributors.md – Medya Gh Jul 17 '18 at 20:01
  • If you like, but no obligation. – Charles Duffy Jul 17 '18 at 20:06

0 Answers0