DEV Community

loading...
Cover image for Some Linux Bash Stuff

Some Linux Bash Stuff

Damon Marc Rocha II
Hi, my name is Damon Rocha. I am a flatiron software engineering graduate. I have been coding for 4 years now.
・2 min read

Bash stands for Bourne again shell. Bash is an interpreted language meaning it does not have to be compiled to convert the code to machine language. This is done by the shell reading the input commands and then finding the corresponding machine commands to replace each line of code.
In this blog series, I want to cover a few of the operations of Bash. Starting with writing, reading, variables, and loops in this article.

Writing

When writing to the terminal you must use the echo command followed by what you want to be written out to the screen encased in double-quotes. An example is shown below

echo "This is Bash"

This is Bash
Enter fullscreen mode Exit fullscreen mode

The output strings can be customized by adding variables that can be determined at runtime. This is shown in the next section

Reading and variables

To read a variable into your bash program simply type read followed by the variable name. Then to access the variable's value put a $ before its name.

read name
echo "Hello $name!"

Hello (value in name)!
Enter fullscreen mode Exit fullscreen mode

In bash, there are only four variable types available to use. These include:

  • Integer variables
  • Strings variables
  • Constant variables
  • Array variables

Declaring and instantiating an integer uses the same process as strings. As for the others, I will cover constants and arrays in a later post.

Loops

The main loop used in bash is a for loop. However, there are a lot of different variations on how to implement this. I will cover the basic structure, and if you want to see more versions look at these docs.

The basic method is to use a range as shown below:

for i in {1..99..2}
do 
  echo $i
done
Enter fullscreen mode Exit fullscreen mode

The numbers in brackets tell the script to loop from 1 to 99 by twos and output the current number. The range can be replaced by any number of multiple items, arrays, integers, or strings.

Discussion (0)