echo $?
will tell the exit code of the last command
- exit code of
0
indicate the program is successful $?
will only get exit code of the last command, need to use it beforeecho
exit
command in a bash script will force exit a program and everything after it will not run
set -e
will exit the script on error
set +e
will do the opposite, reset the option of set -e
Data Streams
stdout
standard output: output that is printed to the screen that doesn’t result in an error
stderr
standard error: stout that is returning failed. eg. permission denied, file not found
stdin
standard input: what the user inputs
read
command is used to ask user for stdin
read -p "what's your name: " yourname && echo "Hello $myname"
-p
option allows for a prompt eg. what’s your name- the stdin is stored in a variable
yourname
DS Redirection
>
is used to redirect streams
>>
is for appending streams
2> /dev/null
will send stderr into dev null, which will hide the stderr message
2
is error1
is stdout, not error- by default
>
only redirect stdout (1), if number not given, 1 is implied
&> target
will redirect both stdout and stderr to the target &
indicates both 1 and 2
Redirection can be separated
find /etc -type f 1>stdout.txt 2>stderr.txt
- this command will redirect stdout and stderr to different locations separately