I want to learn programming. What language should i pick to write cli apps for linux?
I'm interested in C or Go, but i'm open to ideas.
I have plenty of sh scripts i created to integrate my tools and system, so i have some experience and i don't want a scripting language like python.
My first plan is to learn the basics of the language and rewrite some of those scripts.
I think my first pick will be a script that uses ffmpeg to convert my flac files to mp3 or opus. I use sndconv -opus/-mp3 and it checks if there are flac files in the folder (i only have full albums), converts and puts in a folder named "$artist - $album".
My long term goal is to make a cli/tui music player like cmus.
UPDATE: i'm having plenty of success with Go right now. I just wrote a basic version of my music conversion script. It's just converting a music i pass as argument to mp3, but i'll keep working on it and adding functionality just to dip my toes in Go. It seems like a good language and i'm having fun!
Thanks for all the answers!
You're saying you don't want to learn python but in what you're describing, python is the best fit. It's a great beginner language, scales fantastically well to a career (unlike bash), can lead you into a community tackling serious problems and with lots of opportunities in ML, AI etc.
If you want to write a CLI app in Python, I recommend the Click library. It's beautiful to work with. Although your "first script" sounds like it's better suited as a bash script.
The script i want to reimplement is just for learning. It already works well and it's not really needed.
I'm not seeking a career in programming exactly. I like it more as a hobby and i think writing a music player like cmus in python would not be ideal. I particularly don't like the language for cli apps because i like these apps to be as lightweight and simple as possible.
When i switched from ranger (python) to nnn (C) and lf (Go) (terminal file managers), the difference in performance was astonishing.
But thanks for the response. I'll look more into python.
I think you have the wrong perception about python's performance, it's tens of time slower than C, but unless you're processing massive amounts of data, the performance will be irrelevant to you. The task you mentioned for example should have no perceptible difference between a C and a python version, but it's much easier to write in python than in C. Can you really tell the difference between something taking 10 microseconds and 100? When all you doing is passing messages around, python is perfectly suitable.
That said, between python, go and C, there's really no wrong choice. Each language has it's pros and cons.
I don't know, but ranger (python) takes a lot more time to show the contents of folders and to initialize than nnn (C) and lf (Go) even in my recent laptop. More so in a raspberry.
There are many, many possible reasons for the difference in speed beyond language choice. You can write fast Python; you can also write slow C or Go.
You could try Nim, it's fast as C but writes more like python, but is also better designed (as far as I can tell):
I was convincing myself to learn Click, but they really don't recommend using it with Python 3.
argparse(which is now in the standard library since Python 3.2) to be very nice for writing CLI drivers.
Then use it like this:
Why? Where did you see that?
You're not looking at the same docs as me. Did you end up on documentation for a massively outdated version? Click supports 3.x just fine. I've only ever used it with 3.x.
I'm sorry, please help me understand what you mean by Python 3 support.
Is this not valid documentation?
These issues affect any and all command line parsers in python 3, including the stdlib argparse, because of the way python treats sys.argv. Realistically they won't affect you unless your arguments are super weird.
I see. I didn't get that impression from the docs. Further question: would you use Click to make a text editor? ;)
Not unless it's a text editor with a lot of command line arguments. Otherwise argparse works just fine. (argparse is a great library really, so much so that I even use it in JS: https://www.npmjs.com/package/argparse)
Click is super nice for argument-driven command line apps. For example, my battle.net authenticator BNA uses it: https://github.com/jleclanche/python-bna/blob/master/bin/bna
You can look at that file to get a feel for how simple click is. It allows me to very, very clearly and simply define behaviour for not just arguments but commands.
If I wanted to implement
gitin python, I'd use click for the CLI.
Cool. I’ll probably use blessings.
What about a super-duper simple CLI task manager?
Like even simpler than what people considered a simple nowadays? Even simpler than Steve Losh's t
Same answer ;)
I'll agree with Adys.
Start with Python. Learn data structures. Learn control structures. Learn some algorithms. Learn how to build modules and functions. If for some reason you cannot get over using Python, then use Ruby.
Once you've got a hang of writing code in an interpreted language (and start interpreted because having a REPL makes life easier when you're getting the knack of things) and have written a moderate number of useful applications in an interpreted language, then start trying to tackle a compiled language.
For that, I'd lean more strongly towards Rust and Go over C because you'll get a lot of niceties around importing libraries, dependency management, and such which are a gigantic PITA in C.
If you really, really want to get into C, then start with Rust. Rust is hard and will kick your butt, but learning how to write Rust well will make you write better C/C++ because you'll be thinking about memory and safety far earlier than C/C++ expects you to.
For Rust I recommend the docopt library. It will allow you to write your program's CLI interface as a help manual, and automatically parse out the required fields into a struct.
Example from my BrainFuck JIT compiler:
I always had an easier time grocking Ruby over Python, but at the same time I heard it's kind of on the wane as a language with Python being more established and Kotlin/Go being the next gen solutions. Should I just bite the bullet and focus on Python or something like Java if I ever wanted to be a serious developer or is Ruby still pretty established?
Ruby is still pretty established. It's no longer the shiny new thing, but Rails is solid, Chef and Puppet still use a lot of Ruby, and anyone who uses GitHub pages is getting HTML from Jekyll.
Having bounced between Ruby and Python a bunch in the last 6-7 years, there's a lot of similarities between Ruby and Python 3. It can take me about an hour or two to remember the particular syntax, but it's not hard.
So, if you spend a bunch of time learning Ruby because you like the syntax better, you won't be wasting your time. The concepts you'll pick up around data structures, functions, classes, and objects are easily transferrable to most any other language. As a bonus, even if you end up working in a Kotlin job, you'll always have Ruby in your pocket to bang out some automation code or toss up a simple Sinatra site.
Personally? I love Ruby.
Go, sure, but is Kotlin really that popular outside of Android development?
I love Java, but I'm not sure I would recommend anyone learn it today. It certainly doesn't have the halcyon glow of universal enterprise endorsement anymore. Java is getting so little love today that it feels like it's not even the most popular JVM-targeting language anymore.
I honestly don't know. I just tossed it in because OP quoted Kotlin as something they were thinking about learning.
I just pay attention to buzzwords, honestly.
Fully agreed with your post.
You say that you want to write CLI programmes but then you talk about scripts. Scripts are best written in, well, a scripting language. I would prefer POSIX Shell or Ruby, but Python is fine as well, I guess. When you'll want to write a CLI programme, I would recommend Go. Package
flagfrom Go's standard library is seriously under-appreciated. For TUI, package
github.com/gdamore/tcellis fairly popular these days, with lots of examples and stuff built on top of it.
I talked about rewriting one of my scripts in a programming language just as an exercise to learn the basics, a short term goal, but it's not necessary.
That's why i didn't want to learn python. I have plenty scripts already. One for converting music, i have a bulkrename script to open filenames in $EDITOR in a list so i can edit the names, save and it will rename the files, a status bar using lemonbar and a simple password manager.
The end goal is to make a music player similar to cmus.
I'm gonna look at tcell. Thanks!
I'm already using the flag package for command line flags and it's really nice and straightforward.
I'll agree with the other users. Python is perfectly adequate for CLI scripts. It even comes with a competent argument parsing library. And technically, it's actually typically smaller in file size than a complete binary, since basically every distro comes with a python installation now, so you just provide the script.
It is a worthwhile exercise at some point to do C, but only if you're serious about programming. You'll learn to love garbage collectors, and all the other pleasantries modern languages provide, that's for sure.
If you're really dead set on "performance" (almost certainly you'll be bottlenecked by I/O in this case anyway, so it does not matter), Go is a good balance. Rust if you want C-like performance without clawing your eyes out in valgrind and gdb. C++ if you want C while still clawing your eyes out but at least having sane data structures and smart pointers. Java/C#/Kotlin in a similar niche to Go.
I know C a little because i programmed PIC microcontrollers to automate a ruler printing machine in the past.
Thanks for the tips!
I'm going to go against the grain and say that if you're trying to make a music player, one that's relatively small/uncomplicated go with bash. The reason I say this is if you want to be able to easily interact with your system, which it seems you want to, then by far bash is going to be the easiest. Sure it might not be as pretty as other languages but dang does it make it easy to create commands, run other commands, and interact with files. Python you will have to deal with a few layers of abstraction, while bash is WYSIWYG. If you want a more programming and less script related language though, I would recommend python, but beware that this could cause you to have to install a whole lot more dependencies than simply using bash, making it possibly harder to transfer over to other systems if you don't use a tool like pipenv.
I think the problem will be creating a database with the music. I want something similar to cmus with a library showing artists and albums.
Bash has arrays and associative arrays. You can use them then store them in a file and source it.
I agree with everyone else about python, it's a real, serious business programming language. Google, reddit, and tildes all use it. The performance impact isn't really relevant for most desktop apps, but I'd also look into using mpd for your music player, it's really nice, and it has good libraries for c and python
Out of C, Go and Rust, start with Go.
C is old language that is no longer widely used for any projects (unless you’re building a game, OS, or are sircmpwn), and even then C++ is more used. Those languages are old and unnecessarily complicated for a beginner. I advice you to stay away of them as much as you can. They are good if you want to mess with memory yourself, but you don’t want to do that while learning programming. And there are many nasty things that would just make you hate programming.
Rust is great language and I love it, but I can’t recommend it to you. It’s simply too hard to use. It’s as performant as C and seeks to replace it - which it does, Mozilla is writing new parts of Firefox in Rust, Google and Microsoft announced they are looking for secure fast language as C replacement - which Rust is [todo: source]. It has all the benefits of C but none of the cons of messing with memory yourself (which leads to awful, almost undebugable bugs and security vulnerabilities (about 80% of all Windows sevurity vulnerabilities are because of memory issues [todo: source]). The big problem is that you can’t use it as normal language, it has many constructs that no other language has, and it’s very different from any other language ever created. While I love it, I don’t think it’s a good language for a beginner because of it’s difficulty.
I don’t know anything about Go, but it should be nice and fast to learn. So maybe try it.
Just one small detail; C is still very common in the embedded world. It's useful because it has a very small footprint and is fairly low-level.
But then again, things are changing now that everything is IoT; so many things are just using an SoC with Linux running on it.
Golang, easy to pick up, very robust.
I have a CLI use case that I think brings up an interesting (albeit maybe niche) consideration for which language to choose: interactive tools for a multi-user Linux shell environment.
A while back, I set up a little (tiny) shell-based social club at rawtext.club with the goal of it being a free playground for people who like to tinker with shell programming and Linux command line utilities. Well one of the things you would expect to do in a place like this is to write tools or games through which users can interact. But this becomes a challenge if users need to write data to central locations. What you need is a setuid program. And here's the problem: for security reasons, scripting languages usually cannot be run setuid. You need to use compiled binaries from langauges like C if you want to make them setuid. There are workarounds like kicking off a script from a setuid binary wrapper or running the script as a local UDS or other server, but these just add another layer of complexity.
My strong preference is for something like Python for CLI tools, but go with a compiled langauge if you think you'll ever need to make your program setuid.