What shell is the closest to C/C++ syntax?

I am new to the Linux world, and I have been using Windows and writing Visual C++ for the past year or so. I find that bash seems to be the most popular shell in the Linux world. Is there a Linux CLI shell that would be the closest to bash capabilities but with C/C++ types syntax?

10

2 Answers

There's CSH which fits the bill.

From Wikipedia

The C shell (csh or the improved version, tcsh, on most machines) is a Unix shell that was created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been distributed widely, beginning with the 2BSD release of the BSD Unix system that Joy began distributing in 1978.2 Other early contributors to the ideas or the code were Michael Ubell, Eric Allman, Mike O'Brien and Jim Kulp.

Man page

Or you could try TCSH (Thanks! @muru)

From Wikipedia

tcsh (/ˌtiːˈsiːʃɛl/ "tee-cee-shell", /ˈtiːʃɛl/ "tee-shell", or as an acronym "tee cee ess aitch") is a Unix shell based on and compatible with the C shell (csh). It is essentially the C shell with programmable command-line completion, command-line editing, and a few other features. Unlike the other common shells, functions cannot be defined in a tcsh script and the user must use aliases instead (as in csh).

Note that these shells are not as widely used as bash and hence some makefiles and shell scripts may behave unpredictably.

Man page

4

Bash supports some C-style syntax. For example:

  • Function declarations / definitions are sintactically similar to function declarations / definitions in C, except you dont't define neither parameters (which are fetched using the positional parameters $1, $2, $3, ...) nor return types (Bash doesn't use types at all):

    foo() { # ...
    }

    Or again similarily to C using an inline declaration / definition:

    foo() { #...; }

    To be noted that when using an inline declaration / definition the first and last statement must be space-separated from { and } respectively and that (again similarily to C) every statement must be semicolon-separated from the others and that the final statement must end with a semi-colon:

    foo() { command1; command2; command3; }

    For completeness, an alternative way of declaring / defining function is using the keyword function, which doesn't need the () after the function's name:

    function foo { #...
    }
  • You can evaluate C-style expression by enclosing them in double parenthesis (()): quite a number of C-style operators are supported, (including assignment), e.g.: =, >, >=, ==, !=, <, <=, &&, ||, !, among others:

    $ i=0
    $ j=1
    $ ((i > j)) && echo True
    $ ((i >= j)) && echo True
    $ ((i == j)) && echo True
    $ ((i != j)) && echo True
    True
    $ ((i < j)) && echo True
    True
    $ ((i <= j)) && echo True
    True
    $ ((i && j)) && echo True
    $ ((i || j)) && echo True
    True
    $ ((\! i)) && echo True
    True
    $ ((\! j)) && echo True
    $ ((i = j))
    $ echo $i
    1
    $ echo $j
    1

    This way of evaluating C-style expressions can be used in conjunction with if, while and until statements:

    if((i == 0)); then # ...
    done
    while((i == 0)); do # ...
    done
    until((i == 0)); do # ...
    done

    Most notably, (()) also allows to write C-style for loop conditions:

    for((i = 0; i < 3; i++)); do # ...
    done
  • C-style expressions evaluation doesn't support assigning the result of the evaluation; for that you can use arithmetic expansion ($(())), which allows for some sort of C-style expressions assignment:

    $ i=0
    $ j=1
    $ k=$((i > j))
    $ echo $k
    0
    $ k=$((i < j))
    $ echo $k
    1
    $ x=$((1 + 2 + 3))
    $ echo $x
    6
  • C-style prefixed / postfixed increment / decrement operators are also supported:

    ++i
    i++
    --i
    i--
8

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like