604-726-1119

info@enetsolutions.ca

Regions BC, Vancouver Island and Alberta

SURREY :-#508-509 13761 96 Ave, BC V3V 0E8
CALGARY :Unit 123, 5120 47 St NE Calgary, AB T3J 4K3

Bash: A primer for more effective use of the Linux bash shell

There are lots of sides to bash and much to know before you’re likely to feel comfortable snuggling up to it. This post examines many aspects of this very popular shell and recommends further reading.

Network World / IDG

Bash is not just one of the most popular shells on Linux systems, it actually predates Linux by a couple of years. An acronym for the “GNU Bourne-Again Shell”, bash not only provides a comfortable and flexible command line, it delivers a large suite of scripting tools—if/then commands, case statements, functions, etc.—that allow users to build complex and powerful scripts.

This post contains a collection of articles about important aspects of bash that will help you make better use of this versatile shell.

Commands vs bash builtins

While Linux systems install with thousands of commands, bash also supplies a large number of “built-ins”—commands that are not sitting in the file system as separate files, but are part of bash itself. To get a list of the bash builtins, just type “help” when you’re on the bash command line. For more about built-ins, refer to “How to tell if you’re using a bash builtin”.

Scripting in bash

The first line of a bash script is usually #!/bin/bash and indicates that the script will be run using bash. If you run a bash script while using a different shell, this line ensures that bash—and not your current shell—will run the commands included in the script. It is often referred to as the “shebang” line in part because the exclamation point is often referred to as a “bang” (Linux users prefer one syllable to five).

0 of 28 seconds, Volume 0%

Learning to write complex bash scripts can take a while, but “Learning to script using bash” can help you get started.

If you would like to run commands in bash and ensure that they work as intended while saving both the commands entered and the output generated, use the script command. The resultant file will be named “typescript” unless you supply an alternate name with a command like script myscript. “Using the script command” can provide more information.

Using pipes

One of the things that makes the Linux command line so powerful and effective is the way it allows you to pass the output from one command to another command for further processing.

For example, a command like the one below will generate a list of the running processes, strip it down to only those that contain the string “nemo”, remove the lines that include the grep command and then select only the second field (the process ID).

$ ps aux | grep nemo | grep -v grep | awk ‘{print $1}’

Each pipe, represented by the | sign, sends the output from the command on its left to the command on its right. There is no limit to the number of pipes you can use to achieve the desired results, and “Using pipes to get a lot more done” explains their use in more detail.

Aliases

You can turn a complex Linux command into a script so you don’t have to type it every time you want to use it, but you can also turn it into an alias with a command like this that displays the most recently updated files.

$ alias recent=’ls -ltr | tail -8’

Once you save an alias in your .bashrc file, it will be available whenever you log into the system. “Useful aliases” provides more information on creating and using them.

Exit codes

Linux commands generate exit codes—numbers that can tell you whether the commands ran successfully or encountered some variety of error. An exit code of 0 always means success. Any other means there was a problem of some kind. “Checking exit codes” shows how to display them and explains how they work.

The true and false commands relate to exit codes, and “Using true and false commands” shows when and how to benefit from them.

Bash functions

Bash provides a way to create functions, groups of commands that can be set up inside a script and invoked as a group. Functions can therefore be run as often or as infrequently as needed. “Using functions in bash to selectively run a group of commands” shows how to do just that.

Creating a quick calculation function” explains how to easily use a function to facilitate a series of mathematical calculations.

Brace expansion

Brace expansion is a clever trick that allows you to generate a list of values from a simple command. For example, you could generate a list of the numbers 1 through 10 using a command such as echo {1..10} or run through every letter in the alphabet with echo {a..z}. These values could then be used to drive a loop. “Bash brace expansion” will walk you through the process.

Concatenating strings

There are a number of ways in bash to join strings together into a single string. “Concatenating strings” runs through a number of ways to make this happen.

Break and continue

If you’ve never used the break (exit a loop entirely) or continue (jump back to the top of a loop) commands in bash, you will want to read “Using break and continue to exit loops”, which explains how to use them.

Using bash options

Bash options can be helpful with activities such as troubleshooting and identifying variables that are used, but haven’t been defined. “Using bash options to change the behavior of scripts” will show you how to use a series of special bash options to change how scripts behave.

Using eval

Using the eval command”  allows you to run the value of variables as commands.

Bash on Windows

If you use Windows as well as Linux, you might want to consider “Using bash on Windows”.

Clever tricks

No matter how well you learn to use bash, you’ll probably still have many interesting tricks to learn, including this “Half a dozen clever command line tricks”.

Wrap-Up

Bash is a wonderful shell and one that likely offers a lot of options—maybe some you haven’t yet tried. I hope the list of articles included in this post will prove useful to you.

https://www.networkworld.com/article/3679268/bash-a-primer-for-more-effective-use-of-the-linux-bash-shell.html

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Info

© 2022. All Rights Reserved by E-net Solutions