Unexpected `then' bash script

How do I fix this error? I can't see anything wrong with my syntax.

ipcheck() {
  echolog "[INFO] Enabling IP Forwarding..."
  sudo echo 1 > /proc/sys/net/ipv4/ip_forward
  if[$(cat /proc/sys/net/ipv4/ip_forward) == "0"]
    echolog "[CRITICAL] Could not enable IP Forwarding!"
    exit 0
  echolog "[INFO] IP Forwarding successfully enabled!"

I know this is a very basic script, but it's part of a bigger one. The error happens on the then statement.


Shell scripting tends to be a lot more whitespace sensitive than you might be used to if you've come from other programming languages (read: C). Your if line has the problems. You are probably looking for:

if [ $(cat /proc/sys/net/ipv4/ip_forward) == "0" ]

The thing to remember here is that [ is not part of any special if syntax - it's the name of a program (sometimes a shell builtin). If you think of it like that, you can see how the command line parser needs it to be separated. Similarly, the [ command (or builtin) expects the closing ] to be separated from its other arguments, so you need a space before it, too.

Need Your Help

Fluent NHibernate: LEFT OUTER JOIN … AND Query

c# nhibernate fluent-nhibernate

I have the next Fluent NHibernate query using QueryOver:

How do I convert a char to a String?

string char rust

Is there a succinct way to convert a char to a String in Rust other than:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.