Table of Contents
Understanding shells and their varieties
The term „text shell” may sound like a relic from the past. However, in reality, a text shell is an indispensable tool, that enables direct, complex, yet precise and clear interaction with the operating system. In a world of visual icons and touch interfaces, text shells remain invaluable for those, who truly want to understand and control their computer, as well as safe time and resources, which is particularly crucial in it commercial projects. Depending on users’ preferences and needs, shells can be divided into two main families. Graphical shells focus on visual interactions, where elements like cursors, touch, and icons take center stage. Examples of such shells include:- Aqua (typical for macOS)
- Windows Shell
- Gnome, KDE, and Unity, found in various Linux distributions and other operating systems.
- Familiar to Windows users, cmd.exe and PowerShell
- For Linux, Mac and other Unix-like, we have options like sh and its numerous variations, including popular choices like bash and zsh. In the open-source world, various derivatives of text shells allow for customization to specific requirements.
System shells and their key advantages
Graphical shells:
- Lower entry barrier for users but at the cost of reduced functionality*
- Capable of triggering command-line operations in response to various events, such as keyboard shortcuts.
- Often, the graphical interface serves as a user-friendly wrapper for programs designed for text-based environments
Text shells:
- Consistent operation across different operating systems
- The ability to manipulate windows and programs managed by the graphical shell, provided the graphical shell offers appropriate interface
- By adhering to communication standards, text shells make it easy to integrate your own programs and scripts with others, greatly expanding capabilities
- Easy orchestration of multiple programs using relatively simple scripts:
💡Note: Both the advantage and disadvantage of text shells lie in their syntax Advantage: The syntax is incredibly convenient when integrating various commands (compared to regular programming languages). Disadvantage: Logical or arithmetic operations, complex data processing, and analysis are not particularly efficient or fast with text shell syntax. Often, the shell needs to invoke a separate program to handle data analysis. In other cases, the complexity of the syntax can be a hindrance. So, what can you do to be more efficient?
💡Our PRO tip: It’s often better to write a Python or Java script (starting from JDK 9, jshell is available, enabling script execution) for data processing and then connect these programs using a shell script.
Shells emerging from the unix environment
The most well-known examples are 'bash’ and 'zsh,’ with the latter gaining popularity recently. Each of them has its uniqueness, history, and applications, but they all share one common trait — enabling deep, text-based communication with the machine. By getting to know these shells, you increase your chances of quickly transitioning between systems and writing universal scripts that adapt to all types of systems. It’s valuable to understand the syntax differences between bash, zsh, and the basic sh because, at times, on stripped-down systems like embedded systems, only simple implementations like 'ash’ are available, which don’t provide as many features as the more popular bash or the increasingly popular zsh. You can usually install more feature-rich and extensive shells, but the question is, is it worth it?”💡Our PRO tip (for the Windows Environment) In the Windows environment, text shells are also available by default. Characteristic of this family of systems are 'cmd’ and 'PowerShell. It’s highly beneficial to familiarize yourself with the basics of both PowerShell and cmd.exe because sometimes you may not have the option to install other text shells on Microsoft systems. These two typically Windows-native tools complement each other, allowing you to work around limitations of one shell by using the other.
Scripts and commands — in theory
Commands:
In the world of text shells, we encounter two fundamental categories of commands: shell commands and system commands. Shell commands, the first category, are specific to a particular shell and are only available within its scope. For example, the 'cd’ command, while present in many shells, may function slightly differently depending on the specific environment. On the other hand, system commands are independent programs installed on our system. We can execute them from any shell, and their behavior remains consistent regardless of the shell we use. This is a crucial distinction that helps us understand how we communicate with our system through a text-based interface.💡Our PRO tip: You can check whether a specific command is a system command using the 'which’ command in shells similar to 'sh.’ In PowerShell, you can use 'gcm’ or 'Get-Command.’
Scripts:
A script is a standardized set of steps to be executed. It can be executed manually, by typing or copying commands or automatically, by specifying a file containing an organized set of commands to run. While manual input or copying of commands is possible, the real magic happens when we use automation. Sometimes, this even comes with a pleasant sense of excitement. One of the greatest advantages of text shells is the incredible ease of scripting. (However, it’s worth keeping in mind that the deeper you go into the woods, the more trees, wild animals, or even goblins you might encounter!) Everything that we enter directly into the terminal can be saved in the exact same form in a text file. Subsequently, this file can be executed as a single command, allowing the entire set of instructions to run automatically. This not only saves time, but also minimizes the risk of errors resulting from manual command entry. Scripts are a powerful tool in the hands of every shell user.Why scripts are a great solution
- In most cases, users can simply run a script without the need for manual intervention
- Scripts are flexible and allow modifications when the need arises
- They enable the preparation of a runtime environment for more complex programs, that perform additional functions
- Sequential execution of a set of commands guarantees significant process optimization, leading to improved efficiency and productivity
- Automation ensures consistency and eliminates the risk of errors resulting from manual transcription of instructions, further saving time and resources
Guide to scripting for dummies
- What you copy, type, or often input manually into the console – save it as a script
- Create scripts – even the most clumsy ones! Each one of them represents knowledge on how to tackle a specific problem: you don’t have to search the internet on how 'if’ statements or loops work when you have an example on your computer
- Learn from your own work: Copy (with understanding 🙂 ) ready-made solutions from your own scripts
- Keep your scripts in a separate directory. Why? If a piece of code needs to do something elsewhere, it can always navigate to that path using 'cd’ or 'pushd/popd,’ and separate directories help maintain order
- Share your knowledge: Share your scripts as a repository. This not only allows you to revisit them, but also helps others benefit from your expertise.
- Don’t be ashamed! A script is a tool, not necessarily a finished product. It’s also a form of note-taking on how to tackle a specific problem
- Create directories:
- Personal – for what’s universal.
- Company – for company projects.
- Client – for client projects. Sharing insights with clients also helps organize our own knowledge, and our colleagues can benefit from it
- Allocate 20% of your time to automate 80% of a given task — Remember! We don’t always have the time to fine-tune everything, and it’s not always worth it when creating tools for personal use
💡Our PRO tip: The Baby Steps Method!
- Automate one step (the most annoying and simplest one) first.
- Then automate the next one.
- Try to trigger the automated steps one after the other (either as a single script or by calling other scripts).