Mac OS X,  Mac OS X Server,  Ubuntu,  Unix

Separate commands on one line in Bash

In bash, you can run multiple commands in a single line of a script. You do so by separating them with a semi-colon (;). The great thing about this is that if you end up using a variable, you can pass it on to subsequent commands. Here, we’re going to string three commands together and then echo the output:

a=1;b=2;c=$a+$b;echo $c

because we told c to be $a + $b, the $a expands to 1 and the $b expands to 2, we throw them together and then echo out the contents of c$ which appears as follows:

1+2

Now, we could have this thing do math as well, by wrapping the mathematical operation in double-parenthesis, which bash treats as an arithmetic expansion:

a=1;b=2;c=(($a+$b));echo $c

The output this one is simply 3.