In my .bash_profile I have created alias to commands that I run often.
What if I want to group together multiple commands together, and they should run in serial one after the other.
How would I do this?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityYou can create a function instead of an alias:
function foo {
cat somefile.txt
rm anotherfile.txt
}
You can even pass parameters (foo somefile.txt
) and use them as arguments to the commands (e.g. cat $1
for the first argument).
This approach is more flexible than creating an alias
.
.sh
extension, that starts with #!/bin/bash
or #!/usr/bin/env bash
, and is invoked like any other program and contains shell commands and program invocations. The advantage to that is that it's available even in environments that don't load your .bash_profile
, like other shells, can be copied like regular files, and work without editing the .bash_profile
of every user who wants to use them.
Concatenate them with &&
, for example
cat somefile.txt && rm anotherfile.txt
Note that chaining it this way the commands depend on the exit state of their preceding one, so if any command would fail, the execution of the whole line would stop at that point.
git
and there's something wrong when adding files to the local repository, why would you want to push "nothing" afterwards?
&&
is the way to go