Site icon GIXtools

Bash command cheat sheet

Bash scripting

Developing scripts in bash (short for Bourne-Again SHell) can be a time-saving activity. Once your scripts are written and tested, you can depend on them to do a lot of work with little or no effort. This bash command cheat sheet will help you get started with bash scripting.  

The bash shebang line and file permissions 

First, bash scripts should begin with the line #!/bin/bash (sometimes referred to as the “shebang line”) to ensure that bash will be used to run them even if the person using the script is using a different shell. Execute permissions should be assigned with the chmod command so that only the proper groups can run it, as shown in the examples in the table below. 

IDG

Variables and arguments 

Arguments are values passed to a script when you run it. For example, if you type myscript Tuesday 11AM, the script can use those day-of-week and time-of-day values. Set up as many arguments as you need to control how your script behaves. 

Bash commands: variables_and_arguments 

IDG

You can refer to variables as $1, $2 and so on within a bash script. Assigning their values to named variables, on the other hand, generally makes scripts more readable. 

weekday=$1
time=$2

Comments 

Comments in scripts can be very helpful to anyone wanting to understand how the scripts work or update them. Comments follow a # which ensures that anything on the right side of the # is not executed as a command. 

Bash commands: comments

IDG

Using quotes 

Single and double quotes can be used in Linux commands. In the table below, the quotes are needed in the third and fourth examples. Otherwise, the apostrophe (single quote) would leave the shell waiting for a second single quote to end the text. 

Using quotes 

IDG

Note also that the date command enclosed in parentheses in that last command will be run by the shell. 

Looping commands 

Linux provides several ways to loop within scripts – for, while and until. The examples below illustrate how these loops are used. 

 

Looping commands

IDG

Comparison operators 

Comparison operators allow you to compare values. In the looping commands above, the < (less than) and != (not equal) are used for a couple tests. One of the things you need to keep in mind when using comparison operators is that the comparison operators are different for numeric and string variables. 

bash comands numeric_comparisons 

IDG

bash comands numeric_comparisons 

IDG

Using if commands 

The if command is invaluable in bash scripts. It serves as the primary way to determine what action to take based on the value of some variable or condition.  The table below includes some examples that are a little more complex than those shown in the previous two tables. 

bash if_commands 

IDG

Conditional execution 

When you run a command like one shown below, the command on the right will be run only if the command on the left succeeds. This is called a “logical AND”. 

$ tail myfile && echo myfile is available

When you run a command like the following command, the command on the right is run only if the command on the left fails. 

$ echo hello || echo goodbye

Case statements 

The case statement is one that makes it easy to execute any of a long series of commands based on the value of a variable or result of a command. Case statements are concise and very readable. 

Bash case_statements

IDG

Brace Expansion 

Brace expansion, as expressed in the commands below, provides an easy way to loop through a series of values. 

$ for num in {1..3}; do
>   echo $num
> done
1
2
3

Note that it’s also possible to use brace expansion multiple times in the same loop. Note the use of outer and inner braces. 

$ echo {{1..3},{7..11}}
1 2 3 7 8 9 10 11

Substring manipulation 

There are several ways to extract substrings in bash scripts. One of the easiest is to a bash command like this: 

$ target="my cat Bella"
$ echo ${target:7:6}
Bella

You can use a command like the echo command shown below to capitalize (replace) the first letter in a string. 

$ name="joseph"
$ echo "${name/j/J}"
Joseph

Bash commands wrap-up 

This cheat sheet for scripting in bash provides command examples along with explanations of how these commands work. Comments and suggestions are welcome. 

Source:: Network World

Exit mobile version