0

I have two apps who need to be ran in different ways, but I'm so used to my bash alias to start them, so I would like to combine it

One command is ./bin/dev, and another is bundle exec rails s

So I tried to do this, but that doesn't do what I want.

./bin/dev || bundle exec rails s

This one would start bin/dev if available in that folder, but when I cancel (ctrl+C), it would "continue" the chain and run the 2nd command.

I only want the 2nd command to run if the first one fails. Is this possible (one-liner bash alias)

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
Frexuz
  • 4,732
  • 2
  • 37
  • 54
  • 1
    BTW -- there's very rarely any good reason to use aliases (I've been doing this for decades, and can count the number of valid reasons to use aliases I've ever seen on my fingers). Instead of `alias foo='bar baz'`, `foo() { bar baz "$@"; }` defines a function, which gives you far more control; for example, you can run `foo() { if condition; then bar baz "$@"; else qux "$@"; fi; }` and inject your function's arguments at multiple positions, not just at the end. Functions also work in scripts and don't force you to complicate your syntax with an extra layer of quoting. – Charles Duffy May 22 '23 at 15:24
  • @pynexj sorry wasn't clear. I meant that there .bin/dev wouldn't exist at all "no such file or directory" – Frexuz May 23 '23 at 01:40

1 Answers1

2

Use if/else so that you only conditionalize on the existence of ./bin/dev, not its execution

if [ -x ./bin/dev ]; then ./bin/dev; else bundle exec rails s; fi
Barmar
  • 741,623
  • 53
  • 500
  • 612