Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am forming a new question based on an earlier one I posted here in regards to a BASH Script I am writing. This is only one function of the script but I started here because I figured I would have best success with completing this function over others.

The code that I have thus-far is:

welcome="- Sudo Bypass Package Installer -";
echo $welcome;
pkgFetch() {
    echo -n "Name of package you would like to install: "; read pkg
    chkPkg=$(dpkg -s $pkg|grep installed); echo "The Package [$pkg] is already installed."
if [ "" == "$chkPkg" ]; then
    echo  "The Package [$pkg] is installing..."
    sudo apt-get install $pkg -qq
    echo  "The package [$pkg] was successfully installed."
echo  -n "Press ENTER to return to command-line."

The first part, which checks if the package is installed (if it is the script returns the message stating it's already installed) appears to be working correctly. However, I encounter a few things here that I can't make sense of...mostly because I am a novice.

  • If [$pkg] is not installed, the script still displays the message saying that it is, followed by the message that should be displayed if not installed, which is that it is currently installing.
  • The script doesn't silently install the package. It shows that it is reading database, unpacking $pkg, processing triggers, and setting up $pkg. Afterwards, the script displays the correct message that "The package [$pkg] was successfully installed".

Anyone want to take a shot at it here and educate me about my errors?

Thanks in advance : - )

share|improve this question
up vote 2 down vote accepted

the message saying, that the package is already installed is completely unconditional, it is displayed wether the grep returns successful or not.

You could prepend it with [ -n "$chkPkg" ] && to make it conditional (on a nonempty chkPkg variable), or even better pull it into the if block like that:

if [ "" == "$chkPkg" ]; then
  echo  "The Package [$pkg] is installing..."
  # ...
  echo "The Package [$pkg] is already installed."

BTW. You should match variables in shellscript with a single =, not with ==. The latter is not posix conform and will not run in some unix shells apart from bash. Such things are called "bashisms", avoid them where it doesn't inconvenience you too much.

share|improve this answer
Thanks man, my mind was rattling with that. I was brought up learning C++ which is why I used the == rather than the =. This right now is just going to be a personal script used specifically with bash. I'll clean it up as the script progresses. Not clear on the "nonempty chkPkg variable" part of your answer. I tried putting into vim if [ "$pkg" == "$chkPkg" ]; then ... but it displayed $chkPkg as part of the written string rather than a variable. – Kevin Wyman Oct 28 '12 at 10:39
it might be helpful to know herer, that [ is a program (yes, the one you use in an if statement). See man [ or man test to learn what I mean by nonempty. – Paul Hänsch Oct 28 '12 at 15:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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