0

I want to optimize a bash code using 1 line instead of two this is the lines i want to optimize:

grep -E "$name" /etc/passwd
if [ $? -eq 0 ]
     #...

so the if will test the exit of the last command (grep), i want to merge "grep -E "$name" /etc/passwd" in the if statement to have something like:

if [ ##### -eq 0 ]

thanks for your help :)

2 Answers 2

3

Without if:

grep "name" file && do_something
Sign up to request clarification or add additional context in comments.

Comments

2

You can use the exit code of the command directly in the if statement:

if grep .... 
then
  echo "found"
else
  echo "not found"
fi

If you want to silence the output of the grep command you can add the -q option, and if you want to quit after the first match (saves time on large files) you can use -m 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.