Do all different Linux distributions have the same command lines?

8,553

Solution 1

I'm choosing to interpret this question as a question about the portability of commands and shells across various Linux distributions. A "command line" could mean both "a command written at the shell prompt" and "the shell itself". Hopefully this answer addresses both these interpretations of "command line".


Most Unix systems provide the same basic utilities for working at the shell prompt. These utilities are working largely in the same way since they are standardised. Also, the syntax used for writing shell commands is standardised (loops, redirections, pipes, background processes, variable assignments, quoting etc.) The standard is called POSIX and may be found here (see the "Shell & Utilities" section).

On most Unices (especially on Linux for some reason), the standard utilities have been extended with extra functionality, but the functionality described by the POSIX standard should be implemented. If a standard utility does not conform to the POSIX standard, you should probably file a bug report about this.

In particular, the shell itself is often extended to give a more convenient interactive experience, or to be able to provide more advanced shell programming facilities. The shell, being an application like any other, comes in various flavours (implementations) and bash is the most popular on Linux systems (but it's also available as the default shell on e.g. macOS and may be installed on any Unix). The zsh and ksh shells are also popular and provide different sets of extensions, but all should at least be able to do largely what the POSIX standard says using a common syntax (except when using extensions such as special types of arrays and fancier forms of filename pattern matching etc. although some of this happens to be fairly similar between shells too).

As for non-standard tools, such as tools for doing some specific task that is not covered by the POSIX standard (such as talking to a database or adjusting the brightness level of a monitor), or that are specific to a particular Linux distribution (maybe for doing package management), to a version of a particular Linux distribution, or to a particular hardware architecture etc., the portability of the command would depend on the correct variant and version of the tool being installed on a system that supports using that tool.

Across various Linux distributions, the assortment of available tools and utilities is fairly homogenous, and portability is generally good (with the caveat that distribution and architecture specific tools may be different or missing). When looking at using and writing scripts that should work on other types of Unix systems, it becomes more important to know about what extensions are particular to the GNU/Linux variation of tools and utilities, and what can be expected to work on "a generic POSIX/Unix system".

Solution 2

Linux is just a kernel, not OS, things you use directly like commands or GUI programs or beautiful desktops are not provided by the kernel, but by userland components, which are distributed with OSes.

Commands you used in the shell comes from shell builtin functions or external software package. Depending on the softwares you installed, they can be same or different. Although you always use commands in your shell, but they actually have nearly nothing to do with your shell(This even apply to Windows/OS X/*BSD), just learn about how your shell work.

It's just some distros will default-ly install some common software for you, so you get some common commands available. Like you can use ls on almost all distros just 'cause almost all distros install the linux-utils software for you.

In some distros, you have more freedom to choose the packages you want, in that case, you may not get ls available if you refuse to install linux-utils or any software that can provide this command. Also notice that different software packages MAY conflict with each other if they provide commands with the same name.

Solution 3

How a command line is interpreted depends on the shell that is running and how the called program interprets its arguments.

There's a number of different shells, and a command line using a particular feature of one shell may not work on another shell.

That said, in general, the various Linux distros all include at least the same basic shells, so if you can make sure you are executing the same shell, you can use the same command line. (That's why in shell scripts you usually put the needed shell in the first line).

Edit

Look at the shell scripts e.g. in old-style /etc/init.d, and each of those starts with

#!/bin/sh

So these expect some sort of standard shell, which e.g. on Debian is provided by dash. To compare, the shell scripts I write for myself start with

#!/bin/bash

because I sometimes use bash extensions. There's a ton of other shells, see e.g. here

Share:
8,553

Related videos on Youtube

Kaung Sett
Author by

Kaung Sett

I've general knowledge of here and there like programming, networking, enterprise systems, etc. The one I'm pursuing right now is Linux system administration. And that's all. Cheers!

Updated on September 18, 2022

Comments

  • Kaung Sett
    Kaung Sett over 1 year

    Do all different Linux distributions have the same command lines? What I want to know is the same command line works for all kinds of Linux distributions (CentOS, Fedora, Ubuntu, etc.) or whether they all have different command lines?

    • 炸鱼薯条德里克
      炸鱼薯条德里克 over 5 years
      This is a hard-to-find-on-internet question, just like any beginner-level questions, asked with some misunderstanding about general level system structure/layers. It's like no community welcome such questions, causing too much pain for beginners.
  • Kaung Sett
    Kaung Sett over 5 years
    So, to put it in a nutshell, there may be some basic standard command lines which works in all of the linux system whereas extra/extended ones do not.
  • Kusalananda
    Kusalananda over 5 years
    @KaungSett If the command uses only standard tools and no extended features, then there is a higher probability that it is portable. Between homogenous types of Unices, such as various Linuxes (that are using the same implementation of the tools), it is more probable that the command is portable, depending on what it actually is it's doing.
  • Kusalananda
    Kusalananda over 5 years
    @KaungSett An example of what you're thinking of would be nice. It's all very generic otherwise.
  • Kaung Sett
    Kaung Sett over 5 years
    @ Kusalananda Thanks for your answer. TBH, I've just started learning the basic of Linux and this question got pop up in my head. I've tried to google it though but none of them seems to answer my question so here I am.
  • Kusalananda
    Kusalananda over 5 years
    @神秘德里克 There are extensions, such as the -i flag to some implementations of sed and the -t flag for variants of mv and cp. These extensions of standard tools that enables a slightly different functionality. One version of the a tool may not have a particular extension (e.g. "associative arrays" are available in bash 4.0, but not in older releases). Whether a utility is built into a shell or not is often unimportant (any utility could potentially be provided as a built-in utility), likewise whether it's provided as a function or as a script or a compiled binary executable.
  • 炸鱼薯条德里克
    炸鱼薯条德里克 over 5 years
    You didn't get my meaning… I'm talking whether you have command (won't get command not found)because of software installation or function definition. You're talking about the functionality of a command because of its implementation or version. Both are correct.
  • Kaung Sett
    Kaung Sett over 5 years
    Could you please explain a little bit more on "That's why in shell scripts you usually put the needed shell in the first line" ? I didn't get it.
  • Della
    Della over 5 years
    The first line in an executable script supposed to run on a unix based system should look like #!/usr/bin/env <interpreter name>. This is called an interpreter shebang and informs the system which interpreter should be used to translate the rest of the script. The interpreter can be bash, dash, csh, perl or any number of other scripting languages. For example, a python script's first line should look like '#!/usr/bin/env python3'. Granted, you can invoke the script with the interpreter, when the shebang is just a regular comment. But still it is considered a good practice to include it.