Your post actually contains 2 questions.
The -e flag instructs the script to exit on error. More flags
If there is an error it will exit right away.
The $? is the exit status of the last command. In Linux an exit status of 0 means that the command was successful. Any other status would mean an error occurred.
To apply these answers to your script:
egrep "^username" /etc/passwd >/dev/null
would look for the username in the /etc/passwd file.
If it finds it then the exit status $? will be equal to 0.
If it doesn't find it the exit status will be something else (not 0). Here, you will want to execute the echo "doesn't exist" part of the code.
Unfortunately there is an error in your script, and you would execute that code if the user exists - change the line to
if [ $? -ne 0 ]
to get the logic right.
However if the user doesn't exist, egrep will return an error code, and due to the -e option the shell will immediately exit after that line, so you would never reach that part of the code.
-ein the man page (I really want to see an answer address this).$?contains the last exit code (that of theegrepprocess spawned above).-eis documented underset.if egrep -q "^username" /etc/passwd ; then echo "doesn't exist" ; fiifand[. This script won't work with-e, because ifgrepdoesn't find anything then under-ethe script will terminate right there. Without-e, you've got the message backwards: a status ($?) of 0 means that grep did find the user. Note that this should be `grep '^username:', by the way (what if there's another user with a longer name?).#/bin/bash -ehave the same effect as#/bin/bashon line #1 andset -eon line #2 ?