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 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?

share|improve this question
2  
I'm not sure why you would want it, though. Not even Windows' shells (cmd.exe, powershell) have a C-like syntax. – muru Mar 12 at 8:11
    
@muru Because I am very familiar with C syntax and I don't need to deal with a steep learning curve at this point in time. Maybe later down the road I will look into the pros any cons of the other shells available and use whichever fits my needs at the time. – RedDogAlpha Mar 12 at 8:20
13  
That's a bad way to go about this. You're assuming a steep learning curve - yet you don't give any weight to how differences between actual C syntax and a shell's approximation of it could screw you over. Take the plunge, unless you're doing complex scripting, bash is pretty easy. Here's the TLDP Bash beginner's guide - it's old, but good for a starting point. – muru Mar 12 at 8:23
4  
1  
Well, then, all I can say is that the C shell has the worst reputation among shells I have heard of. If you found bash less than pleasant, don't believe (t)csh will be better just because they have C-like syntax. – muru Mar 12 at 16:24
up vote 11 down vote accepted

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

share|improve this answer
3  
Tcsh for the modern world. – muru Mar 12 at 8:10
4  
It is always preferred to add information locally rather than just links, as you are new I did it for you, see how much more useful that is. – Mark Kirby Mar 12 at 9:11
    
@markkirby thanks, I'll remember this when I write answers in the future – gokul_uf Mar 12 at 9:23
    
It should be noted that csh is not only less used, but widely regarded as less suitable for scripting in general, so if a bashism or POSIX-ism doesn't work in tcsh, it is unlikely the author has provided a csh equivalent. – Kevin Mar 13 at 0:57

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--
    
share|improve this answer
    
Spaces in assignments in shell scripts says var = 23 is not valid. Why is while((i \!= 0)); do... done; okay? – cat Mar 12 at 15:38
2  
@tac Well that's not an assignment, it's a comparison (and by the way you don't need to escape !). However yeah, ((i=5)) would work. The culprit is with assignments of the type you mention (x=foo), because by surronunding the = with spaces (x = foo) that wouldn't be considered an assignment anymore, but rather a command x called with arguments = and foo. Does this clear that up? – kos Mar 12 at 15:49
    
This seems to be the way to go for me. Are there any limitations within bash using this method in comparison to using standard bash syntax? – RedDogAlpha Mar 12 at 16:53
2  
@RedDogAlpha (( )) is strictly for arithmetic - everything is treated as an integer. – muru Mar 12 at 16:55
    
@muru Well that's a big limitation! – RedDogAlpha Mar 12 at 16:56

Your Answer

 
discard

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.