C or Bash?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
StevenC21

C or Bash?

Post by StevenC21 »

Which skill is more useful for a Linux user. Assume they are mutually exclusive, as in, you can only know and be proficient in one. Also, explain why; I want to increase my understanding. If this is the wrong forum please direct me to a good place for this.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29532
Joined: Wed Jul 06, 2011 3:58 am

Re: C or Bash?

Post by xenopeek »

I depends entirely on the user in question. What are their goals or needs for learning a programming language.

For general users, not programmers, shell scripting (Bash) will likely be the more useful as the learning curve is much shorter. It gets them comfortable on the command line and lets them automate things with scripts. If they want to get into programming more complex things then obviously shell scripting will sooner rather than later be too limiting.

Want to get into kernel development or systems programming? Then C is the tool of choice. Though for systems programming there are a lot of other choices depending on needs and wants. Want to get into application programming? I'd opt for recommending Python 3 instead as that offers a much more useful standard library than C does. It gets a lot more done in a lot less lines of code.
Image
StevenC21

Re: C or Bash?

Post by StevenC21 »

When I said a Linux user, I suppose I should have been more specific. I did mean a standard user.
MintBean

Re: C or Bash?

Post by MintBean »

It really depends what you're trying to achieve.
User avatar
karlchen
Level 23
Level 23
Posts: 18188
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: C or Bash?

Post by karlchen »

Being a Linux (Mint) user does definitely not require any knowledge of the programming language C.
Knowing bash and knowing C are not mutually exclusive.
The scope of what you use them for are pretty different.
Image
The people of Alderaan have been bravely fighting back the clone warriors sent out by the unscrupulous Sith Lord Palpatine for 762 days now.
Lifeline
StevenC21

Re: C or Bash?

Post by StevenC21 »

I know they aren't exclusive; I meant, in this hypothetical scenario, if you could only have one, which would you want?
User avatar
karlchen
Level 23
Level 23
Posts: 18188
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: C or Bash?

Post by karlchen »

Bash. - You need a command interpreter. You do not need a programming language.
Image
The people of Alderaan have been bravely fighting back the clone warriors sent out by the unscrupulous Sith Lord Palpatine for 762 days now.
Lifeline
User avatar
xenopeek
Level 25
Level 25
Posts: 29532
Joined: Wed Jul 06, 2011 3:58 am

Re: C or Bash?

Post by xenopeek »

Did you intend this to be a kind of poll? We're taking this as a serious question from you because you posted here.
Image
StevenC21

Re: C or Bash?

Post by StevenC21 »

I meant it seriously. But anyways, are polls not acceptable here?
User avatar
xenopeek
Level 25
Level 25
Posts: 29532
Joined: Wed Jul 06, 2011 3:58 am

Re: C or Bash?

Post by xenopeek »

They would be more suitable in the chat forum.
Image
FreedomTruth
Level 4
Level 4
Posts: 443
Joined: Fri Sep 23, 2016 10:19 am

Re: C or Bash?

Post by FreedomTruth »

It really, really, really depends on what you are doing. bash is probably easier to use and quicker to get from pseudocode (concept) to actual working code, but it's certainly not always the best tool. Anecdotal case in point: I recently started working with csv-type data that has to be updated daily (there's a "main" file then smaller "update" files given each day); with my bash script this was taking 1-1.5 hrs to process each day. Granted, I'm not the best scripter and I'm sure my code could have been optimized to some extent; but after I rewrote it in c, the updating process now takes around 5 minutes. Working with large data sets, if speed is an issue, C is probably the way to go. For simple "do this command then do that command" type stuff, go with bash. I still have other bash scripts using the same data that I will probably not move to C because it works fine as-is, and doesn't seem worth it to rewrite the same thing.

If you could only use one, would you use a hammer or scissors?
User avatar
Arch_Enemy
Level 6
Level 6
Posts: 1491
Joined: Tue Apr 26, 2016 3:28 pm

Re: C or Bash?

Post by Arch_Enemy »

StevenC21 wrote: Tue Apr 10, 2018 3:56 pm When I said a Linux user, I suppose I should have been more specific. I did mean a standard user.
Basically it depends what you are trying to accomplish.

C is more geared for putting together packages and can be used for writing small programs to accomplish functions.
Bash scripting, while very powerful in itself, is more geared for putting together strings of commands (this is oversimplified, it is pretty powerful). Think of BASH more along the lines of DOS .bat files.
I have travelled 37629424162.9 miles in my lifetime

One thing I would suggest, create a partition as a 50G partition as /. Partition the rest as /Home. IF the system fails, reinstall and use the exact same username and all your 'stuff' comes back to you.
User avatar
xenopeek
Level 25
Level 25
Posts: 29532
Joined: Wed Jul 06, 2011 3:58 am

Re: C or Bash?

Post by xenopeek »

Arch_Enemy wrote: Wed Apr 11, 2018 10:48 amDOS .bat files
This statement likely only means something to people older than 35 :)
Image
User avatar
Arch_Enemy
Level 6
Level 6
Posts: 1491
Joined: Tue Apr 26, 2016 3:28 pm

Re: C or Bash?

Post by Arch_Enemy »

xenopeek wrote: Wed Apr 11, 2018 10:52 am
Arch_Enemy wrote: Wed Apr 11, 2018 10:48 amDOS .bat files
This statement likely only means something to people older than 35 :)
I fit that category quite aptly...
I have travelled 37629424162.9 miles in my lifetime

One thing I would suggest, create a partition as a 50G partition as /. Partition the rest as /Home. IF the system fails, reinstall and use the exact same username and all your 'stuff' comes back to you.
User avatar
Termy
Level 12
Level 12
Posts: 4254
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: C or Bash?

Post by Termy »

I'd absolutely opt for shell programming, and I did, and continue to do so; it might not give you nearly the flexibility of compiling languages like C, but it sure as heck makes life a lot easier than typing out thousands upon thousands of lines of code to get tasks done of which shell could make light work, which is especially useful in Linux and similar environments, like Mac. I'd also suggest Python, as an alternative option, were that possible.

For a 'standard' user though, as mentioned later in this thread, I'm not sure why they'd be learning to program in shell, let alone C. :lol:

Shell is far more powerful than most seem to realise, but having a good idea of its limitations is important as well. I think languages like Python seem to be a good halfway between effort and capability, which is why I sometimes use that, but I'm not that great at it. Shell, however -- now that's my passion, alongside Linux of course.

Ultimately though, I agree with other comments in this thread: it depends on the user.
I'm also Terminalforlife on GitHub.
lmuserx4849

Re: C or Bash?

Post by lmuserx4849 »

StevenC21 wrote: Tue Apr 10, 2018 11:42 am Which skill is more useful for a Linux user. Assume they are mutually exclusive, as in, you can only know and be proficient in one. Also, explain why; I want to increase my understanding. If this is the wrong forum please direct me to a good place for this.
Linux Kernel is the operating system (OS).

A Linux Distribution is the Linux OS plus additional layers of system and application software that run in the user space.

Folks use the term "linux" to mean a fully functioning computer, but that even means something different if you are talking about desktop, server, or even a IOT device. Linux is everywhere.

The shell is just one of those layers. Bash is one of many shells (Korn shell (ksh), C shell (csh), etc). When you type a command at a console you are using Bash.

Bash is not a programming language, it is a command interpreter. It is an interface between the user and the OS. It has program-like constructs to enable you to test the exit status of commands or process command output.

When you type: ls -l *.jpg, it is bash/readline that will process that line. The asterisk is called a glob (not to be mistaken for regular expression), each word is read in and expanded as necessary based on the IFS.

# Compiling a C progam from the command line could look like (you just used bash):
gcc myCpgm.c -o mycpgm && echo success || echo failure

Even if you use an IDE to develop your application and it hides these details from you, it is best that you learn what the blackbox is doing on your behalf.

How do you know which shell you are running: echo $0 or ps -p $$

Bottom line
If you are a regular user and only intend to use GUI's, you don't need to know bash or C, but imho you are limiting yourself and putting up unnecessary barriers to successfully using and exploiting all that a Linux system has to offer. You are missing out why many of us chose linux. It is fun. It is flexible. It is educational. It is challenging.

I'm guessing you are already using bash at the command line. I don't think a C-Programmer could get by without knowing a shell. If you type gcc at a console, you used bash :-)

You only need 3 resources to learn bash:
  1. The Linux Command Line, by William Shotts. You can buy his book or download it. Go through his two tutorials, "Learning the Shell" and "Writing Shell Scripts".
  2. The unofficial Bash FAQ.
  3. The bash man page and/or reference.


After that pick a programming language, C, java, python, even pascal or fortran....

If you want to contribute to an open source project, maybe even LM Cinnamon, I suspect python, C/C++, and/or javascript might be helpful. In most of these cases, the language is the easier part. It's all the libraries and frameworks that are more challenging. You don't learn C/C++ in a vacuum. If you are on KDE, you'd use QT. If you are on GNOME, you'd use GTK, ...
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: C or Bash?

Post by Pilosopong Tasyo »

StevenC21 wrote: Tue Apr 10, 2018 3:56 pmI did mean a standard user.
As a standard user, Bash will suffice. Every Linux user will encounter one form of shell scripting or another, sooner or later anyway.

C -- although touted as a [one of several] general-purpose programming languages -- is more geared towards developing simple as well as complex applications and system software. You deal with data structures and concepts that go beyond shell scripting.

Yeah, shell scripts are like writing DOS batch files... on steroids :lol:
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
Paul_Pedant

Re: C or Bash?

Post by Paul_Pedant »

A late contribution.
You don't truly write a whole lot of real stuff whether you work in Shell or C. The whole secret of complex stuff is reusability. Doing everything from scratch would consume your whole lifetime: you need to leverage other people's work. Imagine making and putting up a shelf if you had to build your own workbench, saws, drills, spirit level, adhesives, paint and screws. Invent your own paper, pencil and yardstick for design? Grow and fell your own tree, and dry the wood?
In Shell, there are a couple of dozen commands. You do actual work by invoking your choices from several hundred utility programs.
In C, there are a couple of dozen language constructs. You do actual work by invoking your choice from a bunch of libraries containing several hundred well-defined functions.
In both cases, knowing the existence, capabilities and parameters of the utilities and functions is much more important than the Shell and C skills. It also helps if you have a generalised overview of data flow and problem solving. Don't think in Shell or C, think first and implement in a specific methodology later.
Personally, I do a lot of text data management, and my go-to is Bash for the framework, embedding GNU/awk for anything complicated or computational. I usually find awk is around 200 times faster than shell, and C is only about 5 times faster than awk, so there is almost no incentive to prototype in awk and then rewrite in C.
Locked

Return to “Scripts & Bash”