Looking for a bit of help as I can't get this to work. I want to test if apt is installed on the system and then create aliases for it if so.

I came up with the following command, but it doesn't work, as I've determined that the [ ] is coming up true, no matter if apt is there or not.

if [ -x $(which apt) &> /dev/null ]; then alias ai='sudo apt install';   alias ar='sudo apt remove';   alias as='apt search';   alias ah='apt history';   alias au='sudo apt update && sudo apt upgrade'; fi   # apt

I want the aliases to be created only if apt is there. Can you think of the correct test that I should be using?


-x is a file test whereas $(which apt) &> /dev/null is a command.

To use -x you could do

if [ -x /usr/bin/apt ]; then

If you want to search using which, you could do

if [ -x "$(which apt)" ]; then

however you could instead use the exit status of the which command directly:

if which apt &> /dev/null; then ...

Be aware that which is not always definitive - see for example Why not use “which”? What to use then?

  • Perfect 👍. Thanks for the warning, that's good to know also (though I'm not too worried about that as these are not for enterprise production environments, just some useful shortcuts to get things done a bit more quickly on my home systems. 🙂). – YorSubs Dec 5 '20 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.