Compund commands
Compound commands are a group of commands that are runned in succession
Simple example:
echo “hi” ; echo “there”
This queues up the commands
If you only want the second command to run if the first one succeeds, use &&
Example:
mkdir newfolder && cd newfolder
Lets try it in the root directory where this will fail, the first command will fail
So to make the second command run if the first one failed ust ||
mkdir newfolder || echo “Directory creation failed”
Lets group the 2 commands together
mkdir newfolder2 && cd newfolder2 || echo “Directory creation failed”
If we wan't to execute multiple commands and make them run inside the current shell we use
{ echo “Hi” ; echo “There” ;}
Spaces between curly brace and echo is important, or else the command will not run
This seems to be the same as echo “Hi” ; echo “There” but its not.
Lets output the last echo command to a txt file
echo “Hi” ; echo “There” > hello.txt
Here only the last echo command will show up in the hello.txt file
If we group the together with curly braces, both commands will echo out to the txt file
{ echo “Hi” ; echo “There” ;} > hello.txt
You can do the same thing with parenthesis
( echo “Hi” ; echo “There” ;) > hello.txt
Using the parenthesis the command will run in a subshell
Using the curly braces the command will run in the current shell.