Bash Logic - basic syntax

Pipelines

(this is covered in the bash manual here)

  • A pipeline is a sequence of one or more commands separated by one of the control operators:
    |
    or
    |&
  • The output of each command in the pipeline is connected via a pipe to the input of the next command.
    (if you want you can read more about pipes)
  • These could be both shell builtins and external commands. For example:
    pwd | grep home
  • If | is used, command1 standard output is connected to command2 input
  • If |& is used, it adds also command1 standard error
  • The exit status of a pipeline is the result of the last command of the pipeline.
    For example:
1$ ls -l / | grep bin
2lrwxrwxrwx   1 root root          7 Apr 18  2023 bin -> usr/bin
3lrwxrwxrwx   1 root root          8 Apr 18  2023 sbin -> usr/sbin
4$ echo $?
50
6$ ls -l / | grep Dave
7$ echo $?
81
9$

Lists of Commands

(this is covered in the bash manual here)

  • A list is a sequence of one or more pipelines separated by one of the operators: ;
    &
    &&
    ||
  • It is optionally terminated by:
    ; &
    newline
  • If a command is terminated by the control operator ‘&’, the shell executes the command asynchronously in a subshell. This is known as executing the command in the background.
    The shell does not wait for the command to finish, and the return status is 0 (true).
  • Here's an example of a list made of 2 pipelines:
1$ ls / | grep bin ; echo hello | wc -c
2bin
3sbin
46
5$
  • Here's an example of a list with a command that ends with &.
    This will create a job that can be controlled (we'll not cover jobs here)
1$ echo start ; sleep 30 &   echo stop 
2start
3[1] 7395
4stop
5$ jobs
6[1]+  Running                 sleep 30 &
7$ jobs
8[1]+  Done                    sleep 30
9$

AND and OR lists

  • AND and OR lists are sequences of one or more pipelines separated by the control operators ‘&&’ and ‘||’, respectively.
  • AND and OR lists are executed with left associativity.
  • An AND list has the form:
    command1 && command2 command2 is executed if, and only if, command1 returns an exit status of zero (success).
  • An OR list has the form:
    command1 || command2 command2 is executed if, and only if, command1 returns a non-zero exit status.
  • The return status of AND and OR lists is the exit status of the last command executed in the list.

AND and OR lists - EXAMPLES

  • example 1: Using ||, first pipeline is successfull (so second one is not executed).
1$> echo hello || ls /
2hello
3$> echo $?
40
5$> 
  • example 2: Using ||, first pipeline fails
1> ls dave || ls / | grep bin
2ls: cannot access 'dave': No such file or directory
3bin
4sbin
5$> echo $?
60
7$> 
  • example 3: Using &&, first pipeline is successfull
 1$> ls / | grep bin && ls 
 2bin
 3sbin
 4 bar-ilan   Documents   look_place             Music      shmulik     ttt
 5 cd-guest   Downloads   minikube-linux-amd64   Pictures   snap        Videos
 6 Desktop    files       moshe                  Public     Templates  'ystemd basics](https://www.yuval.guide/linux/systemd/systemd-basics/)'
 7$> 
 8$> echo $?
 90
10$> 
  • example 4: Using &&, both pipelines fail:
1$> ls / | grep dave && ls dave
2$> echo $?
31
4$> 

Grouping Commands

  • When commands are grouped, redirections may be applied to the entire command list.
  • For example, the output of all the commands in the list may be redirected to a single stream.
  • Grouping with ()
    • ( list )
    • Placing a list of commands between parentheses forces the shell to create a subshell , and each of the commands in list is executed in that subshell environment.
    • Since the list is executed in a subshell, variable assignments do not remain in effect after the subshell completes.
  • Grouping with {}
    • { list; }
    • Placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created.
      The semicolon (or newline) following list is required.