2

My tab completion (BASH in debian) shows me different numbers for combining two commands subsequently. It's somehow common sense that I can execute more commands the more privileges I have, e. g. with sudo.

Obviously, the ways for concatenating with ; and && are slightly different.

  • What's the difference between them?
  • Why does the difference in numbers decrease for accessible commands for users with root privileges? I would expect an increasing number (difference). (most probable/most common reason)
nepumuk@nepumuk:~$  
Display all 2216 possibilities? (y or n)
nepumuk@nepumuk:~$ date ; 
Display all 2214 possibilities? (y or n)
nepumuk@nepumuk:~$ date && 
Display all 2216 possibilities? (y or n)
nepumuk@nepumuk:~$ date ; sudo 
Display all 2729 possibilities? (y or n)
nepumuk@nepumuk:~$ date && sudo 
Display all 2730 possibilities? (y or n)
nepumuk@nepumuk:~$ date ; man 
Display all 8127 possibilities? (y or n)
nepumuk@nepumuk:~$ date && man 
Display all 8127 possibilities? (y or n)

EDIT 1: I also noticed that the "number of possibilities" increases by one or two when I do this enquiry and start it all over again (except for the amount of manuals, which is somehow forseeable)…

So I did it some times for encountering a reproducible behaviour:

nepumuk@nepumuk:~$
Display all 2221 possibilities? (y or n)
nepumuk@nepumuk:~$ date ;
Display all 2221 possibilities? (y or n)
nepumuk@nepumuk:~$ date &&
Display all 2221 possibilities? (y or n)
nepumuk@nepumuk:~$ date ; sudo
Display all 2735 possibilities? (y or n)
nepumuk@nepumuk:~$ date && sudo
Display all 2735 possibilities? (y or n)

EDIT 2: I even can't run some commands with sudo "enabled":

nepumuk@nepumuk:~$ sudo alias
sudo: alias: command not found

So the number (more exact: the set) of commands does differ whether sudo is used.

9
  • If you have the "programmable completion" enabled (shopt progcomp), bash will try to complete not only commands, but also command line switches (and guess --most of the time wrong ;-) -- when some commands and filenames cannot be used).
    – user313992
    Commented Jan 12, 2020 at 13:16
  • I never used shopt progcomp. Maybe it's a default option…?
    – Nepumuk
    Commented Jan 12, 2020 at 13:20
  • shopt progcomp will tell you if it's enabled or not. It's enabled by default on eg. debian.
    – user313992
    Commented Jan 12, 2020 at 13:20
  • How to know wether it's enabled?
    – Nepumuk
    Commented Jan 12, 2020 at 13:21
  • 1
    By using shopt progcomp -- it will answer with progcomp on or progcomp off. You can use shopt -s progcomp to enable it, and shopt -u progcomp to disable it.
    – user313992
    Commented Jan 12, 2020 at 13:23

1 Answer 1

2

I believe that the "possibilities" being shown are not necessarily programs that you can run. For example, when you type man, it is likely saying that there are that many possible things you can run man with (e.g. man ls). The number of commands that you can run should be very similar regardless of whether you're root or not. If you're not root, you may still run the same programs. The programs will just give an error when they can't acquire root privileges.

As for the difference between && and ;, it's quite simple. && will only run the command on the right if the command on the left completes successfully. ; will run the command on the right regardless of how the command on the left completes. The behavior of && is due to something called Short Circuit Evaluation.

More specifically, && will only run the command on the right if the exit code of the command on the left is zero. The counterpart to && is ||, which will only run the command on the right if the one on the left returns a non-zero exit code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .