From the course: LPIC-1 Exam 101 (Version 5.0) Cert Prep

Simple and compound commands - Linux Tutorial

From the course: LPIC-1 Exam 101 (Version 5.0) Cert Prep

Start my 1-month free trial

Simple and compound commands

- [Instructor] A simple command is any of the commands you type in a regular basis. For instance, ls or grep root /etc/passwd. These are simple commands. When we refer to compound commands, things get more complex. A compound command is a couple of commands that are grouped in some way and are all executed in close succession. We have a couple ways of doing this. We can specify a second command that runs after the completion of our first command. Type clear, then type in echo "hi" ; echo "there", and hit Enter. This will run the first echo command and when it completes, it runs the second echo command. Note that the second echo command runs no matter whether the first command succeeds or not. This is really the same thing as typing in the first command, hitting Enter, and typing in the second command and hitting Enter again. It just queues up the command, so we don't have to wait. If you only want the second command to run if the first command succeeds, you can use the double ampersand symbols. For instance, in your home directory, type in mkdir newfolder && cd newfolder. In this case, we only change into newfolder if its creation was successful. In our case, it was. You can see this in our prompt. Now let's go to the root directory and try the same command again. Type in cd /, and then type in clear. Now bring your line back and hit Enter. In this case, the second command doesn't run because the first wasn't successful since we couldn't make a directory in slash due to lack of permissions. If you want to execute a second command only if the first command fails, then use a double pipe symbols. While we're still on slash, let's try to create a directory again. This time type in clear, then type in mkdir newfolder || echo "directory creation failed" and hit enter. Now only when the mkdir command fails does the echo run. Now let's combine both operators. Type in clear and then type in mkdir newfolder2 && cd newfolder2 || echo "Directory creation failed". In this example, it attempts to make newfolder2 and if successful it cd's into it. If it's not successful, it echo's the error message. Hit enter. Because we're still in slash and can't create a folder, we should've gotten an error message. Now let's go back to our home directory. Type in cd and hit enter. Type in clear, and then bring your line back and hit enter again. This time we should see our prompt change as the directory was created and we changed into it. These are not the only ways of creating compound commands. If we want to execute multiple commands and have them run inside a current shell, we'll use the curly braces. To demonstrate this, let's use the echo command twice. Type in clear and then type in { echo "hi" ; echo "there" ;} and hit enter. The output of this is similar to what we did earlier. You might be thinking this is doing exactly the same thing, but let me explain why it isn't. Let's redirect our first compound command example to a file. Type in echo "hi" ; echo "there" > hello.txt and hit enter. Note that hi ended up on the screen. Now let's view the text file. Type in cat hello.txt and hit enter. Only the word there is in the file. Only the second echo got redirected to the text file. Now let's use curly braces. Bring your line back and add in the left curly brace and then after there add in a semicolon and a right curly brace and then hit enter again. Now we'll view the text file again. Type in cat hello.txt and hit enter. We can see that both lines are in this file. The curly braces force bash to process all commands inside the braces as one command. This would allow us to pipe or redirect the output. Now let's do the same thing but replace the curly braces with parenthesis. Bring your line back again and change the curly braces to a left parenthesis and a right parenthesis and hit enter again. And once again we'll view the text file. We get the same thing as using curly braces. The difference between curly braces and parenthesis is that the latter runs the commands on a sub shell. The curly braces run the commands on the same shell. Since the commands are running in a sub shell they'll have different variable scope. To show this, type in clear, then we'll type in a couple of different commands. A = 0; and for our second group, {a=10; echo "in=$a"); for our last group, echo "out=$a". In the first command, A is assigned to zero. Then inside the sub shell, A is assigned to 10, then we echo inside the sub shell and again outside the sub shell. Hit enter. You can see from the output that these are actually two variables named A. One in the main shell, and one in the sub shell, and they hold different values. If you want to have separate variable space, use parenthesis for a sub shell. If you just want to group commands that execute as one thing, use the curly braces.

Contents