1

I'm new to bash scripting and learning "if then" statements. When I run the simple script below it executes and gives me the correct result however I still get an error. The name of the script is testscript.sh. The error is as follows: "./testscript: line 5: [csh: command not found]"

Can someone tell what this means?

#! /bin/bash

MY_SHELL="csh"

If ["$MY_SHELL" = "bash"
then
    echo "You seem to like the bash shell."

else

    Then "You don't seem to like the bash shell."

fi
  • 4
    Welcome to Unix & Linux. While we are happy to answer any questions regarding Unix/Linux the best method is to perform some investigation prior to asking. In the case of a new learner feel free to link to the tutorial you're following if you are using one. I googled bash if and the first link is a tutorial that should get you everything you need, in particular the String comparisons section at the end: tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html – Centimane Aug 3 '16 at 18:27
10

Let's go through this line by line:

#! /bin/bash

MY_SHELL="csh"

So far, so good.

If ["$MY_SHELL" = "bash"

Now this line has a number of errors:

  1. if and not If - shell is case senstive so foo and FOO are different
  2. There should be a space after the [ character
  3. You need a ] at the end of the test with a preceding space.

This means the line should more like if [ "$MY_SHELL" = "bash" ]

then
    echo "You seem to like the bash shell."
else

These are good

    Then "You don't seem to like the bash shell."

You want an echo here. Remember about case sensitivity.

fi

The result:

#! /bin/bash

MY_SHELL="csh"

if [ "$MY_SHELL" = "bash" ]
then
    echo "You seem to like the bash shell."
else
    echo "You don't seem to like the bash shell."
fi

Edit: once you get more comfortable with shell scripting, https://www.shellcheck.net/ is a useful site to use to validate stuff. The error output isn't always clear, but it's normally good for pointing out mistakes!

  • I doubt the code in the question even matches what the OP ran, because capital-If wouldn't ever try to run [csh as a command. If true;Then echo foo;fi in an interactive shell gives bash: syntax error near unexpected token 'fi'. (Since fi is the first correctly-capitalized part of the whole thing, so it's actually a keyword) – Peter Cordes Aug 4 '16 at 3:20
  • thanks, not sure how the second echo command got omitted, but you answered my initial question though – Randy Teer Aug 4 '16 at 19:54
1

You would need a space after [ and before ] since brackets are actually commands! So:

if [ "$shell" = "bash" ];

There are other errors which I didn't point out since this is the one you asked for.

  • 1
    I would also recommend newbies to use test command as test "$MY_SHELL" = "bash" to get used to thinking of it as I command. And after a while get back to brackets notation . – ddnomad Aug 3 '16 at 18:25
  • -1 for "other errors I didn't point out" ... – ilkkachu Aug 3 '16 at 21:45

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.