What is bash?
Bash or Bourne-again shell is a command interpreter that interprets commands written in what is called a terminal or command interpreter. When we write a command like the following, the command interpreter processes the command and prints the result to the screen.
Table of Contents
The ls command is very commonly used. We use it during almost every transaction in daily life. We will explain this command in more detail later.
ls
Here are the command interpreters called SHELL that do this interpretation process together with the operating system kernel.
Why Bash?
There are also various command interpreters outside Bash such as sh, csh, ksh, tcsh, zsh.
You can use any of these. However, it is useful to use Bash as it comes by default in Linux-based operating systems and is used a lot.
The following command is used to find out the command interpreter used.
echo $SHELL
The Bash command interpreter allows the use of variables, operators, conditional statements, loops, and functions.
Bash Scripts
Sometimes one command is not enough to do what we need. Let’s define a variable and print this variable to the screen.
NAME="Configzone"; echo $NAME;
Since the command is short, we can run it by typing it into the command interpreter.
However, when we want to check a value received from the user and transfer it to a variable or use a loop, the commands will become complicated. In this case, writing commands to a file would be beneficial for readability and reusability.
Save the following simple bash myscript example in any directory as a command.
Important: After this step, we will perform script operations. In order for you to receive errors and warnings in the terminal; It is recommended to do the operations with sudo su authority.
#!/bin/bash
echo "Config Zone"
Then run the script with bash command. Bash script files can be run with permission to be executable.
chmod +x myscript
After the executable permission is granted, it is run in the following format.
bash myscript
./myscripty
After running myscript, you will see Config Zone in the terminal.
The most important part to consider when writing a bash script is the first line. The first line has the shebang (#!) followed by the command interpreter path used. Command interpreter sh, php, perl etc. it could be.
#!/bin/sh
echo "Cofig Zone try sh"
The operating system transmits the commands written to the command interpreter on the first line, and the command interpreter executes the commands.
Comment Line
Comment line is used to make bash commands understandable or to prevent a command from running. When typing in bash, to comment a line, we just put a # before it.
#!/bin/sh
#This line is comment line
echo "Config Zone"
# echo "This line is not work"
Variables
A variable is used to temporarily store data. As in all programming languages, it is very important to use variables.
#!/bin/sh
NAME="Config Zone"
echo $NAME
Single or double quotes can be used when declaring a variable. However, special expressions enclosed in double quotes are interpreted by bash.
!/bin/sh
NAME="Config Zone $((2022 + 1))"
echo $NAME
${#variable_name} is used to get the variable length.
#!/bin/sh
NAME="config zone"
echo $NAME - ${#NAME}
The readonly keyword is used to define a read-only variable.
#!/bin/sh
readonly NAME="Config Zone"
NAME="Config Zone" # Gives a warning.
echo "Welcome, $NAME"
Unset keyword is used to remove the defined variable.
#!/bin/sh
NAME="Config Zone"
unset NAME
echo "Welcome, $NAME"
One of the following methods is used to define an array.
!/bin/sh
CONTACTS=("Config" "Zone" "Script" "Day")
CONTACT_NUM=${#CONTACTS[@]} # number of elements
echo $PERSON_NUM
echo ${CONTACTS[3]} # 4th element
or
#!/bin/sh
PERSON[0]="Config"
PERSON[1]="Zone"
PERSON[2]="Day"
echo ${PERSON[*]} # all elements
NOTE: The first element of the array is accessed with 0. Variables belonging to the operating system can also be accessed with bash script.
#!/bin/sh
echo "user:" $USER
echo "index:" $HOME
The following command is used to learn other operating system variables.
#!/bin/sh
export NAME="Config Zone"
echo $NAME
bash
The variable will be cleared after the operating system restarts. Command results can be stored in variables.
#!/bin/sh
#RESULT=$(ls)
#RESULT=$(date +%Y-%m-%d)
#or
RESULT=`ls`
echo $RESULT
When running a bash script, the filename is $0, the command action number is $$, the parameters are $1,$2,$n, and the parameter number is $#.
#!/bin/sh
echo "Commands ID: " $$
echo "Commands: " $0
echo "Parameter: " $1
echo "Sum: " $#
You can continue with other Bash related topics in the next article. In the next article;
Get value from user
Arithmetic operators
Conditional expressions
Arithmetic operators
Text operators
Logical operators
File and directory operators
3 thoughts on “Linux Bash -1”