Posts

Showing posts with the label Pipestatus

eval: $? vs ${PIPESTATUS[@]} (bash)

 In Bash, `?` and `${PIPESTATUS[@]}` are used to check the exit status of commands, particularly in the context of pipelines. 1. `?`:    - `$?` is a special variable that stores the exit status of the last executed command.    - It's primarily used to check the success or failure of the most recently executed command.    - For example, you can use it like this:      ```bash      some_command      if [ $? -eq 0 ]; then          echo "Command was successful."      else          echo "Command failed."      fi      ``` 2. `${PIPESTATUS[@]}`:    - `${PIPESTATUS[@]}` is an array that stores the exit statuses of all commands in a pipeline.    - It is particularly useful when you have a series of commands piped together, and you want to check the exit status of each command in the pipeline.    - For example:      ```bash      command1 | command2 | command3      exit_statuses=("${PIPESTATUS[@]}")      # Check the exit status of each command      if [ ${exit_sta