Skip to main content
added 13 characters in body
Source Link
nohillside
  • 104.4k
  • 42
  • 222
  • 275

The command as written doesn't really make sense, the command substitution will always return an empty string (and the part after the || will fail for non-root users). If you are looking for a way to add a line to /etc/hosts if there is no entry yet, try

grep -qxF '127.0.0.1        app.local' /etc/hosts ||
    sudo sh -c "echo '127.0.0.1        app.local' >> /etc/hosts"

PS: Relying on the number of spaces in the grep part is riskyasking for trouble, maybe better use a regexp instead

The command as written doesn't really make sense, the command substitution will always return an empty string (and the part after the || will fail for non-root users). If you are looking for a way to add a line to /etc/hosts if there is no entry yet, try

grep -qxF '127.0.0.1        app.local' /etc/hosts ||
    sudo sh -c "echo '127.0.0.1        app.local' >> /etc/hosts"

PS: Relying on the number of spaces in the grep part is risky, maybe better use a regexp instead

The command as written doesn't really make sense, the command substitution will always return an empty string (and the part after the || will fail for non-root users). If you are looking for a way to add a line to /etc/hosts if there is no entry yet, try

grep -qxF '127.0.0.1        app.local' /etc/hosts ||
    sudo sh -c "echo '127.0.0.1        app.local' >> /etc/hosts"

PS: Relying on the number of spaces in the grep part is asking for trouble, maybe better use a regexp instead

Source Link
nohillside
  • 104.4k
  • 42
  • 222
  • 275

The command as written doesn't really make sense, the command substitution will always return an empty string (and the part after the || will fail for non-root users). If you are looking for a way to add a line to /etc/hosts if there is no entry yet, try

grep -qxF '127.0.0.1        app.local' /etc/hosts ||
    sudo sh -c "echo '127.0.0.1        app.local' >> /etc/hosts"

PS: Relying on the number of spaces in the grep part is risky, maybe better use a regexp instead