2

How do I make an alias in my .bashrc such that I can run two commands with arguments?

For example, compile source and run.

gcc-run -lm example.c

would run

gcc -lm example.c
./a.out

The closest I found was this question but it doens't have any arguments.

Community
  • 1
  • 1
mushroom
  • 1,909
  • 3
  • 16
  • 33

1 Answers1

5

If you want to pass an argument, you can't use an alias. You need to use a shell function. It sounds like you want something similar to

gcc-run () {
    gcc "$@"
    ./a.out
}    
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Another small related question. How would I then pick out individual arguments to use in an if-else statement? – mushroom Apr 29 '13 at 19:07
  • 2
    the shell functions are simuliar as "normal" shell program, you can use `$1` `$2`... for individual arguments. – clt60 Apr 29 '13 at 19:10
  • You might actually want to make sure that compilation was successful. `gcc-run () { gcc "$@" && ./a.out; }` – devnull Apr 30 '13 at 05:05